Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL – Creating UTC Timestamp from separate date/time/timezone information

I’m working with BigQuery and have a table that looks like:

YEAR MONTH DAY timezone local time
2015 6 24 America/Los Angeles 1930

Where local time is given by hhmm. I’m wondering if I can format this information into a timestamp column in SQL that yields time in UTC.

I know I can use `TO_TIMESTAMP` but that would involve concatenating all these columns as strings first. Is there any better way to do this? If I were to concatenate, I’m not sure how I would use timezone and then back out UTC.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You might consider below.

WITH sample_table AS (
    SELECT 2015 year, 6 month, 24 day, 'America/Los_Angeles' timezone, 1930 local_time
)
SELECT TIMESTAMP(DATETIME(year, month, day, DIV(local_time, 100), MOD(local_time, 100), 0), timezone) utc
  FROM sample_table;

Query results

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading