How to convert UTC time to local timezones based on timezone column in bigquery?

谁说胖子不能爱 提交于 2021-01-29 17:29:33

问题


I've been trying to convert each UTC time back to the appropriate local timezone using standard SQL in GBQ, but couldn't find a good way to do it dynamically because I might have tons of different timezone name within the database. I'm wondering if anyone has an idea?

The table I have contains 2 different columns (see screenshot)


回答1:


Below example is for BigQuery Standard SQL

#standardSQL
WITH `project.dataset.yourtable` AS (
  SELECT 'Pacific/Honolulu' timezone, TIMESTAMP '2020-03-01 03:41:27 UTC' UTC_timestamp UNION ALL
  SELECT 'America/Los_Angeles',  '2020-03-01 03:41:27 UTC'
)
SELECT *, 
  DATETIME(UTC_timestamp, timezone) AS local_time
FROM `project.dataset.yourtable`

with output

Row timezone            UTC_timestamp           local_time   
1   Pacific/Honolulu    2020-03-01 03:41:27 UTC 2020-02-29T17:41:27  
2   America/Los_Angeles 2020-03-01 03:41:27 UTC 2020-02-29T19:41:27  


来源:https://stackoverflow.com/questions/60824817/how-to-convert-utc-time-to-local-timezones-based-on-timezone-column-in-bigquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!