简体   繁体   English

有没有办法在 BigQuery 中将 ISO 8601 转换为日期格式?

[英]Is there a way to convert ISO 8601 to date format in BigQuery?

I have input from Amazon Alexa in the format of ISO 8601 and was wandering if I needed to do a whole bunch of string substrings & transforms to make it into a BigQuery Timestamp format, or is there some function that does it?我有来自 Amazon Alexa 的 ISO 8601 格式的输入,如果我需要做一大堆字符串子字符串和转换以使其成为 BigQuery 时间戳格式,或者是否有一些 function 可以做到这一点,我正在徘徊?

I also understand that it is hard to turn 2015-W49 into a date, but thought I would ask.我也明白很难将 2015-W49 变成日期,但我想我会问。

references: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/built-in-intent-ref/slot-type-reference#date https://en.wikipedia.org/wiki/ISO_8601#Dates https://code.google.com/p/google-bigquery/issues/detail?id=208参考: https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/built-in-intent-ref/slot-type-reference#date https://en.wikipedia.org /wiki/ISO_8601#Dates https://code.google.com/p/google-bigquery/issues/detail?id=208

This can be done via the function PARSE_DATE as detailed here: https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_date 可以通过PARSE_DATE函数来完成此操作,详情如下: https : PARSE_DATE

In BigQuery Standard SQL: 在BigQuery标准SQL中:

SELECT DATE_ADD(PARSE_DATE('%Y', SUBSTR('2015-W49',0,4)), INTERVAL CAST(SUBSTR('2015-W49',7,2) as INT64)-1 WEEK) as parsed;

I would expect below result of such conversion, which is first day of the respective week, which should be respectively : 我希望这样转换的结果如下,分别是该周的第一天,分别是:

Week Date in ISO 8601   First Day of the week    
2015-W01                2014-12-28   
2015-W02                2015-01-04   
2015-W49                2015-11-29   

You can verify above at http://www.enpicbcmed.eu/calendar/2015-W01 for example 例如,您可以在http://www.enpicbcmed.eu/calendar/2015-W01上进行验证

I think below returns correct result 我认为下面返回正确的结果

#standardSQL
WITH data AS (
  SELECT '2015-W01' AS dt UNION ALL
  SELECT '2015-W02' AS dt UNION ALL
  SELECT '2015-W49' AS dt  
)
SELECT 
  dt, 
  DATE_ADD(PARSE_DATE("%Y-W%W", dt), 
           INTERVAL 7 * CAST(SUBSTR(dt,-2,2) AS INT64) - 6 - 
           EXTRACT (DAYOFWEEK FROM PARSE_DATE("%Y-W%W", dt)) DAY
  ) as d
FROM data
ORDER BY dt

This worked for me:这对我有用:

DATE_ADD(
    DATE_TRUNC(PARSE_DATE('%Y-%m-%d', CAST(date_close AS STRING)), ISOYEAR),
    INTERVAL EXTRACT(WEEK FROM date_close) - 1 WEEK
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM