如何将字符串更改为时间戳?

时间:2020-10-03 13:55:04

标签: google-bigquery timestamp

我的时间戳数据仍为STRING类型,如下所示:

+-----------------------------+
| created_at                  |
+-----------------------------+
| 2019-09-05T07:44:32.117283Z |
+-----------------------------+
| 2019-09-05T08:44:32.117213D |
+-----------------------------+
| 2019-09-06T08:44:32.117283A |
+-----------------------------+
| 2019-09-21T09:42:32.117223T |
+-----------------------------+
| 2019-10-21T10:21:14.1174dwC |
+-----------------------------+

如何将其更改为ISO格式,例如“ 2020-09-05 07:44:32 UTC”?

预先感谢

1 个答案:

答案 0 :(得分:1)

您可以使用PARSE_TIMESTAMP('%FT%T', SPLIT(created_at, '.')[OFFSET(0)])PARSE_TIMESTAMP('%FT%T', SUBSTR(created_at, 1, 19))-随便哪个都可以

您可以使用问题中的示例数据来测试,玩转上面的示例

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '2019-09-05T07:44:32.117283Z' created_at UNION ALL
  SELECT '2019-09-05T08:44:32.117213D' UNION ALL
  SELECT '2019-09-06T08:44:32.117283A' UNION ALL
  SELECT '2019-09-21T09:42:32.117223T' UNION ALL
  SELECT '2019-10-21T10:21:14.1174dwC' 
)
SELECT PARSE_TIMESTAMP('%FT%T', SPLIT(created_at, '.')[OFFSET(0)])
FROM `project.dataset.table`   

有输出

Row f0_  
1   2019-09-05 07:44:32 UTC  
2   2019-09-05 08:44:32 UTC  
3   2019-09-06 08:44:32 UTC  
4   2019-09-21 09:42:32 UTC  
5   2019-10-21 10:21:14 UTC