0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

bigqueryでstring型のUTC日付をJSTに変換する方法

Last updated at Posted at 2021-03-05

bigqueryで string型のUTCをJST変換する手順 を調べてて結構手こずったのでメモを残します。

UTC→JST変換


SELECT
  id
  , created_at  -- ①元データ:これをJSTに変換したい
  , SUBSTR(created_at, 1, 19) as UTC_string  -- ②UTCのstring型で時刻の情報を抽出
  , TIMESTAMP(SUBSTR(created_at, 1, 19)) as UTC_timestamp  -- ③UTCのTIMESTAMP型に変換
  , FORMAT_TIMESTAMP('%Y-%m-%d %H:%M:%S',TIMESTAMP(SUBSTR(created_at, 1, 19)), 'Asia/Tokyo') as JST_timestamp  -- ④JSTのTIMESTAMP型に変換
  FROM
    `dataset.table` 

出力イメージ

id ①created_at ②UTC_string ③UTC_timestamp ④JST_timestamp
123456 2021-03-02 06:59:04.322000 +00:00 2021-03-02 06:59:04 2021-03-02 06:59:04 UTC 2021-03-02 15:59:04

①の元データがstring型
②で、SUBSTRを利用して時刻情報までを抽出する(これをやらないで直接③を行うとエラーになる)
③でUTCのTIMESTAMP型に変換する
④でJSTのTIMESTAMP型に変換する

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?