简体   繁体   中英

Parsing Date time in BigQuery

Below is the date format for the 'date' field, which I'm getting from API to Bigquery as a string. 2020-09-17T00:00:00+03:00 I want to parse the date format to '%Y%m%d' and then extract it as ISOWEEK. This is the code I came up with;

EXTRACT(ISOWEEK FROM PARSE_DATETIME("%Y%m%d", REGEXP_EXTRACT(date, r"...................")))

However, this gives me an error. Could you guide me what needs change? Thank you

Let's break down the steps here. The date input is a string of a date in a certain timezone (+03:00), for example "2020-09-17T00:00:00+03:00" . Then on this string you can apply:

  • Keep the date on the correct timezone
DATE("2020-09-17T00:00:00+03:00", "+03:00")
  • Format the date with the desired format in a string
FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))
  • Parse the date from the previously formatted string
PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00")))
  • Finally extract the ISOWEEK from the date
EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))))

So, this will give you the result you want.

select EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE(date))))

You can also skip the "%Y/%m/%d" if it's not really helping you with something else and simply do

select EXTRACT(ISOWEEK FROM DATE("2020-09-17T00:00:00+03:00", "+03:00"))

which results to the same thing

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