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

时间:2017-01-12 16:25:16

标签: google-bigquery iso8601

我从亚马逊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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

我希望下面这种转换的结果,即相应周的第一天,应分别为:

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上验证以上

我认为下面会返回正确的结果

#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