简体   繁体   中英

Get the date of the weeks monday for a date in BigQuery

I have a set of dates, and I need to get the date of the monday for the same week of the date in BigQuery. Example:

2021-02-07  -->  2021-02-07
2021-02-08  -->  2021-02-07
2021-02-09  -->  2021-02-07
2021-02-10  -->  2021-02-07
...

How can I get this? So far I've been able to get the Weeknumber, but not the date, with the following sql: EXTRACT(WEEK(MONDAY) FROM Fecha)

Consider below simple approach

select date, 
  date_trunc(date, week(sunday)) date_monday,
  date_trunc(date, week(tuesday)) date_wednesday
from your_table    

if apply to sample data in your question as

with your_table as (
  select date '2021-02-07' date union all
  select '2021-02-08' union all
  select '2021-02-09' union all
  select '2021-02-10' 
)           

the output is

在此处输入图像描述

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