Skip to main content

Categories: Date/Time

WEEK

Extracts the ISO 8601 week number (from 1 to 53) for a given DATE or TIMESTAMP value. The week that contains the first Thursday of January is week 1. Returns NULL if the input is NULL.

Syntax​

EXTRACT(WEEK FROM date_timestamp_expression string) → bigint​

  • date_timestamp_expression: A DATE or TIMESTAMP expression.

Examples

WEEK example using a timestamp
SELECT EXTRACT(WEEK FROM TIMESTAMP '2019-08-12 01:00:00.123456')
-- 33
WEEK example using a date
SELECT EXTRACT(WEEK FROM DATE '2019-08-12')
-- 33
WEEK example using the CAST function
SELECT EXTRACT(WEEK FROM CAST('2019-08-12 01:00:00' AS TIMESTAMP))
-- 33
WEEK example for a date in the last week of the previous year
SELECT WEEK(DATE '2021-01-01')
-- 53
WEEK example for the first week of a year
SELECT WEEK(DATE '2021-01-04')
-- 1

Usage Notes​

This function uses the EXTRACT function. When using the CAST function, timestamps containing milliseconds are not allowed.