BigQuery: Convert DateTime in different time zone to UTC - google-bigquery

I'm new to Big Query,
I want to convert the following date-time to UTC timezone.
DateTime format "2021-07-03T23:59:00+04:00[Asia/Dubai]" to UTC.
Thank you.

You just need to convert it to TIMESTAMP() without specifying any timezone (since it automatically sets the datetime at UTC) and then convert it back to DATETIME():
SELECT
your_datetime,
DATETIME(TIMESTAMP(your_datetime)) AS datetime_utc
FROM
<your_table>
OUTPUT:
your_datetime datetime_utc
------------------------- -------------------
2021-07-03T23:59:00+04:00 2021-07-03T19:59:00

Related

Convert cst to pst in big query

I have a datetime in a google data base that is in cst. I would like to convert it to pst.
Queries such as
SELECT DATETIME("2008-12-25 15:30:00+07","America/Los_Angeles") as date;
seem to assume that the starting date is in UTC. How can I convert if the starting date is in cst?
You can convert time in Bigquery regardless of what your starting date to a DATETIME in a specific timezone:
SELECT DATETIME("2008-12-25 15:30:00-06" ,"PST8PDT") as date;
Output:

Timestamp string conversion / from_utc_timestamp

I need to convert 2021-10-03 15:10:00.0 as 2021-10-03T15:10:00-04:00
I tried with.
from_utc_timestamp(from_unixtime(unix_timestamp('2021-10-03 15:10:00.0', "yyyy-MM-dd HH:mm:ss.S"),"yyyy-MM-dd'T'HH:mm:ssXXX"),"America/New_York")
I got Null value
Any suggestions please
from_utc_timestamp can accept timestamp or compatible string (yyyy-MM-dd HH:mm:ss.S), or bigint, not this: "yyyy-MM-dd'T'HH:mm:ssXXX"
Hive timestamps are timezoneless. Once you converted from UTC to America/NY, the timezone information is lost, only you know in which timezone it is, having timestamp converted it is already impossible to derive the timezone from it.
You can concatenate with timezone, conversion like this returns what you need but it works for particular date only. In December -05:00 timezone should be usedm not +04:00:
date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss+04:00") --This is wrong!!!
From_utc_timestamp is Daylight saving aware. It can be -05:00 or -04:00 depending on the date.
Consider this example, first returns 5, second returns 4:
select (unix_timestamp("2020-01-01 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-01-01 12:00:00.0","America/New_York")))/60/60
select (unix_timestamp("2020-10-19 12:00:00.0")-unix_timestamp(from_utc_timestamp("2020-10-19 12:00:00.0","America/New_York")))/60/60
So, you can get current time zone corresponding to America/New_York for the same timestamp and concatenate it with converted timestamp:
select concat(date_format(from_utc_timestamp('2021-10-03 15:10:00.0',"America/New_York"),"yyyy-MM-dd'T'HH:mm:ss"),'+0',
--get hrs shift
(unix_timestamp("2021-10-03 15:10:00.0")-unix_timestamp(from_utc_timestamp("2021-10-03 15:10:00.0","America/New_York"))) div 3600,':00')
Result:
2021-10-03T11:10:00+04:00
It should work correctly with different timestamps taking into account daylight saving time for America/New_York.

convert timestamp timezone to datetime with another time zone in Bigquery

I have an external table (from GCS) in bigquery that by auto scheme detect a datetime column and creates a timestamp column in the table with UTC. The time zone of my data is Central (America/Chicago).
I know that the timezone is central and I want to create another column with UTC timezone. I tried using parse_timestamp/converting to string then to datetime with timezone. But couldn't get it right. Any suggestion?
The following query will output the correct UTC time. Remember that BigQuery stores timestamps in UTC.
with sample as (
select timestamp('2021-01-10 08:00:00', 'UTC') as ts
)
select timestamp(datetime(ts), 'America/Chicago') from sample;

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')

Postgres SQL Timezone conversion

I could appreciate a second pair of eyes on my Postgres syntax.
Database stores timestamp in UTC. I'm trying to convert from UTC to Eastern Daylight Time EDT, but the output is not accurate.
Here's my syntax:
SELECT
to_char(((timestamp AT TIME ZONE 'UTC') AT TIME ZONE 'EDT'), 'MM/DD/YYYY HH24:MI')
FROM table_name
Record TimeStamp:
09/10/2016 12:00
Query Output:
09/10/2016 16:00
Desired Output:
09/10/2016 08:00
Thanks for your assistance.
Saying AT TIMEZONE twice is redundant since it will just convert from whatever the current timezone (which you suggest is UTC) to UTC then to EDT.
The fact that you feel the need to convert it to UTC tells me you're not storing it as a TIMESTAMP WITH TIMEZONE. Check if this is the case. If it is, that's likely your problem. From the docs:
If no time zone is stated in the input string, then it is assumed to be in the time zone indicated by the system's timezone parameter, and is converted to UTC using the offset for the timezone zone.
Basically, if you don't specify, it assumes it's from your current timezone by default, not UTC. It's possible you entered a UTC timestamp and it assumed it was EDT.