PARSE_DATETIME 格式与一年中的某一天 [英] PARSE_DATETIME formatting with day of year
问题描述
BigQuery 中的 PARSE_DATETIME 函数与年份 (%j) 格式元素一起使用时存在问题.该函数似乎忽略了年份元素.
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.
例如
选择 PARSE_DATETIME("%Y%j", "2013243")
返回 2013-01-01T00:00:00
,缺少年份组件.
returns 2013-01-01T00:00:00
, lacking day of year component.
但是具有相同日期格式元素的反向函数按预期工作:
However the reverse function with the same date formatting elements works as expected:
select FORMAT_DATETIME("%Y%j", "2013-02-02T00:00:00")
返回:2013033
错误?还是用户错误?
干杯
推荐答案
我认为这是一个可以修复的错误!它没有逻辑以一种方式工作,但不是相反的!
I think that this is a bug that could be fixed! there is no logic in it working one way but not opposite!
同时,您可以使用下面的方法来实现目标
Meantime, you can use below to achieve goal
#standardSQL
CREATE TEMP FUNCTION PARSE_DATETIME_WITH_DAYS(x STRING) AS (
DATETIME_ADD(PARSE_DATETIME('%Y%j', x), INTERVAL CAST(SUBSTR(x, -3) AS INT64) - 1 DAY)
);
SELECT PARSE_DATETIME_WITH_DAYS('2013243')
结果 -
Row f0_
1 2013-08-31T00:00:00
这篇关于PARSE_DATETIME 格式与一年中的某一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!