我想在Bigquery中将我拼写的工作日和月份转换为时间戳格式。
我的日期目前存储为Mon Jul 27 07:08:00 +0000 2015
,我想将其转换为2015-07-27 07:08:00 UTC
我已阅读How do I cast dd/mm/yyyy string into date in BigQuery?的回答并尝试按以下方式格式化
SELECT TIMESTAMP(year + '-' + month + '-' + day) as output_timestamp
FROM (
SELECT
REGEXP_EXTRACT(created_at, '.*/([0-9]{4})$') as year,
REGEXP_EXTRACT(created_at, '^([0-9]{2}).*') as day,
REGEXP_EXTRACT(created_at, r'(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)') AS month
)
[dataset.tablename] where collected_at = "2016-01-01 00:00:01" LIMIT 100
但是,我收到此错误query: SELECT query which references non constant fields or uses aggregation functions or has one or more of WHERE, OMIT IF, GROUP BY, ORDER BY clauses must have FROM clause. (error code: invalidQuery)
如何将其转换为我想要的格式?
答案 0 :(得分:1)
使用Standard SQL,您有PARSE_TIMESTAMP函数,其中包含大量format elements,因此您可以将字符串转换为TIMESTAMP
,如果以后需要构建自定义字符串它,你可以使用FORMAT_TIMESTAMP
。
在您的示例中,以下格式字符串将起作用:
SELECT PARSE_TIMESTAMP("%a %b %d %T +0000 %E4Y", t) FROM
(SELECT 'Mon Jul 27 07:08:00 +0000 2015' t)
使用的格式:
%a - Abbreviated weekday
%b - Abbrevialted month
%d - Time in HH:MM:SS
%E4Y - 4 digits year