简体   繁体   中英

BigQuery parse date not working on %Y%W format

Everything is in the screenshot: I have a column with date string in format %Y%W . I want to get the date of the Sunday of the corresponding week (eg. 202201 returning 2022-01-09 )

The PARSE_DATE function cannot parse the data correctly, it just return the year correctly but doesn't parse the week number

As from the doc :

The format string fully supports most format elements except for %a, %A, %g, %G, %j, %u, %U, %V, %w, and %W

So how to solve the problem?

Data sample:

week_year
202204
202208
202203
202205
202202
202207
202206

this question is related but doesn't provide answer to the problem

在此处输入图像描述

Ugly but working solution (inspired from this ):

DATE_SUB( DATE_ADD(DATE_TRUNC(PARSE_DATE('%Y%m%d',
          CONCAT(SUBSTR(week_year, 1, 4), '0601')), isoyear ), INTERVAL CAST(SUBSTR(week_year, -2) AS int64) WEEK), INTERVAL 1 day),
  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM