如何将YYYY-MM-DD格式的日期转换为BigQuery Standard SQL中的第一天?
答案 0 :(得分:5)
使用DATE_TRUNC
:
#standardSQL
SELECT DATE_TRUNC('2017-02-17', MONTH);
或者,对于一个更有趣的示例,输入是格式为YYYY-MM-DD
的字符串:
#standardSQL
SELECT DATE_TRUNC(CAST(s AS DATE), MONTH)
FROM (
SELECT '2017-02-17' AS s UNION ALL
SELECT '2017-02-14' AS s
);
答案 1 :(得分:0)
如果输入是字符串,你可以进行字符串操作而不是放入DATE类型,这里有几种可能性:
#standardSQL
SELECT
REGEXP_REPLACE(s, r'({\d}4-{\d}2)-{\d}2', '\\1-01'),
CONCAT(SUBSTR(s, 0, 7), '-01')
FROM (
SELECT '2017-02-17' AS s UNION ALL
SELECT '2017-02-14' AS s UNION ALL
SELECT '1985-08-31' AS s
);