How do I truncate down timestamp in Bigquery - sql

I am trying to truncate UTC time in bigquery. I'm trying to remove the millisecond off my timestamp. I don't want to round down, just remove the millisecond.
To this: 2019-11-11 19:10:57 UTC```
I've tried truncate and date but can't seem to make it work.

Try below (BigQuery Standard SQL)
TIMESTAMP_TRUNC('2019-11-11 19:10:57.181 UTC', SECOND)
this will produce timestamp 2019-11-11 19:10:57 UTC

Related

timestamp difference in bigquery with date

I have used Dataiku to transfer 40 GB of data from snowflake to big query. Somehow timestamp value changed my dates completely
Instead of 2021-06-30 00:00:00 UTC the copied timestamp value is 2021-06-29 22:00:00 UTC
I am looking for a bigquery solution to cast this into the correct timestamp as loading the data again is not possible.
Can someone help please? Thanks
found the solution
SELECT TIMESTAMP_ADD(Day_Sts, INTERVAL 120 MINUTE) AS Day_Sts,* except (Day_Sts)

hive from_utc_timestamp returns wrong time

I am using Hive and wants to get the UTC time.
By running
select date_format(to_utc_timestamp(bigint(1621446734295),'UTC'),'yyyy-MM-dd HH:mm:ss.SSS')
It returns: 2021-05-20 01:52:14.295.
However, this timestamp refers 2021-05-19 17:52:14.295 GMT.
Why does the function to_utc_timestamp still returns time with timezone? Do I need to change some settings for Hive?

How do I prevent Redshift INSERT datetime from dropping the timezone?

I have a String in this format: 2018-11-01T00:00:00-07:00 and I would like to convert it to a TIMESTAMP and insert it into a TIMESTAMP column. However, when I insert it, it drops the -07:00 without first converting it to -00:00. How do I ensure that it is converted and stored in Redshift properly?
Here is an example:
select ORIGINAL_DATE, TO_TIMESTAMP(ORIGINAL_DATE,'YYYY-MM-DD HH24:MI:SS') FROM CDW_LANDING.X where id = XXXXXX;
=> 2018-11-01T00:00:00-07:00 2018-10-31 17:00:00
The TO_TIMESTAMP converts it to 2018-10-31 17:00:00 which is what I want. However, when I insert it, it becomes 2018-11-01 00:00:00 and simply drops the -07:00.
Here is the example:
insert into cdw_stage.X (ORIG_DT)
select TO_TIMESTAMP(ORIGINAL_DATE,'YYYY-MM-DD HH24:MI:SS')
from CDW_LANDING.INVOICE where id = XXXXXX;
But when I query it with select ORIG_DT from cdw_landing.X;, it displays 2018-11-01 00:00:00. What I would like to see is 2018-10-31 17:00:00 which is what the TO_TIMESTAMP function should do.
The ORIG_DT in Redshift is in TIMESTAMP format. The input date is in VARCHAR.
How do I get Redshift to save this correctly? I also added postgres tag because Redshift is based off of postgres. Thank you so much!!!
2018-11-01T00:00:00-07:00 is not a timestamp (timestamp without time zone) literal, strictly speaking. It is a timestamptz (timestamp with time zone) literal. This is the root of all pain in your question. The wrong cast to timestamp ignores the offset. The Postgres manual:
In a literal that has been determined to be timestamp without time zone, PostgreSQL will silently ignore any time zone indication. That
is, the resulting value is derived from the date/time fields in the
input value, and is not adjusted for time zone.
Bold emphasis mine.
The use of TO_TIMESTAMP() can't save you. The Redshift manual:
Formats that include a time zone (TZ, tz, or OF) are not supported as input.
(The same is true in Postgres.)
Solution
Cast to timestamptz (or use a column of that type to begin with), the rest should fall in place:
SELECT cast('2018-11-01T00:00:00-07:00' AS timestamptz);
Or:
SELECT '2018-11-01T00:00:00-07:00'::timestamptz;
The manual about casting in Redshift.
When an actual timestamptz is assigned to a timestamp column it is converted according to the current timezone setting of the session automatically. If you want a different target timezone, use the AT TIME ZONE construct. Details:
Ignoring time zones altogether in Rails and PostgreSQL
The related answer is for Postgres, but timestamp handling in Redshift (while differing in many other aspects!) is the same. The Redshift manual:
When converting DATE or TIMESTAMP to TIMESTAMPTZ, DATE or TIMESTAMP
are assumed to use the current session time zone. The session time
zone is UTC by default. For more information about setting the session
time zone, see timezone.

Converting timezone from UTC using numbers as opposed to names for timezones in BigQuery SQL?

I know that in BigQuery one can convert a timestamp by DATETIME(timestamp, timezone). But the names of the timezones in SQL are very poorly organized. I was wondering if there is a function or a way to convert from a time in
UTC to some other timezone using a string of number like "+00:04" or "4" where the number would indicate the amount of hours the timezone is ahead or behind the UTC time.
Thank you!
You can specify a timezone by supplying its UTC offset using the following format:
(+|-)H[H][:M[M]]
For example:
-07:00
SELECT CURRENT_DATETIME('-07:00'), DATETIME(CURRENT_TIMESTAMP(), '-07:00')

Extracting Time from Timestamp in SQL

I am using Redshift and am looking to extract the time from the timestamp.
Here is the timestamp: 2017-10-31 23:30:00
and I would just like to get the time as 23:30:00
Is that possible?
In Redshift you can simply cast the value to a time:
the_timestamp_column::time
alternatively you can use the standard cast() operator:
cast(the_timestamp_column as time)
Please go through this link
http://docs.aws.amazon.com/redshift/latest/dg/r_Dateparts_for_datetime_functions.html
timezone, timezone_hour, timezone_minute
Supported by the DATE_TRUNC function and the EXTRACT for time stamp
with time zone (TIMESTAMPTZ)
Examples is here
select extract(minute from timestamp '2009-09-09 12:08:43');
select extract(hours from timestamp '2009-09-09 12:08:43');