PARSE_DATETIME formatting with day of year

前端 未结 2 334
太阳男子
太阳男子 2021-01-29 04:30

Having an issue with the PARSE_DATETIME function in BigQuery used with the day of year (%j) formatting element. The function seems to ignore the day of year element.

Eg.

2条回答
  •  温柔的废话
    2021-01-29 05:07

    Not a bug, neither an error! PARSE_DATETIME uses a format_string and a STRING representation of a DATETIME to return a DATETIME -> "2013243" does not represent a DATETIME string, not a DATE...

    To achieve what you are looking for first get the day number - 1 and add it to date (first day of the year) and format the output to DATETIME

    SELECT DATETIME(DATE_ADD((SELECT PARSE_DATE("%Y%j", "2013243")), INTERVAL CAST((SELECT SUBSTR("2013243", -3)) AS INT64) -1 DAY));
    

    Output: 2013-08-31T00:00:00

提交回复
热议问题