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

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

我从 Amazon Alexa 以 ISO 8601 的格式输入,如果我需要做一大堆字符串子字符串和转换以使其成为 BigQuery 时间戳格式,或者是否有一些函数可以做到这一点,我正在徘徊?

我也明白很难把 2015-W49 变成日期,但我想我会问。

参考资料: 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

这可以通过函数 PARSE_DATE 完成,详见此处:https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#parse_date

在 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;

我希望这种转换的结果如下,这是相应一周的第一天,应该分别是:

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   

您可以在 http://www.enpicbcmed.eu/calendar/2015-W01 上面验证,例如

我认为下面returns正确的结果

#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