[英]Extraxt date from ISO Week (201905) BigQuery
I need to extract date of Sunday from it's ISO Week number: ie 201905 It needs to be in #standardSQL as it's going to be scheduled with scripts that don't support legacy.我需要从它的 ISO 周数中提取星期日的日期:即 201905 它需要在 #standardSQL 中,因为它将使用不支持遗留的脚本进行调度。
I tried adjust working formula from Google Sheets but can't figure it out.我尝试从 Google Sheets 调整工作公式,但无法弄清楚。 The original formula from Google Sheets:谷歌表格的原始公式:
TO_TEXT (
(DATE(LEFT(Week_ISO,4),1,1)
- (WEEKDAY(DATE(LEFT(Week_ISO,4),1,1))-2)
+ (RIGHT(Week_ISO,2)-1)*7)
+6)
From my reading of the documentation, this should work:从我对文档的阅读来看,这应该有效:
PARSE_DATE('%G%V', isoyyyymm)
But it doesn't.但事实并非如此。
So, here is an alternative:所以,这里有一个替代方案:
SELECT DATE_ADD(DATE_TRUNC(PARSE_DATE('%Y%m%d', CONCAT(substr(isoyyyyww, 1, 4), '0601')),
isoyear
),
INTERVAL CAST(substr(isoyyyyww, -2) as int64) WEEK
)
FROM (SELECT '200506' as isoyyyyww);
The idea here is the following:这里的想法如下:
You can adapt this if your value is a number rather than a string.如果您的值是数字而不是字符串,则可以对此进行调整。
The other option for me was to just not change to ISOWEEK.我的另一个选择是不更改为 ISOWEEK。 The iso week loses the year information anyway.无论如何,iso 周都会丢失年份信息。 Instead I just floored all dates to the first day of the ISOWEEK.相反,我只是将所有日期都放在 ISOWEEK 的第一天。
DATE_ADD(
LAST_DAY(DATETIME(date), ISOWEEK),
INTERVAL
-6
DAY) as login_week
It seems your proposition is not totally right.看来你的提议并不完全正确。 For instance, it's not working for the week 200101. With your formula, the answer is 2001-01-08.例如,它不适用于 200101 周。根据您的公式,答案是 2001-01-08。
Here is my suggestion :这是我的建议:
SELECT DATE_ADD(DATE_TRUNC(PARSE_DATE('%Y%m%d', CONCAT(substr(isoyyyyww, 1, 4), '0104')),isoyear), INTERVAL CAST(substr(isoyyyyww, -2) as int64)-1 WEEK)
FROM (SELECT '200506' as isoyyyyww);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.