Created
September 30, 2022 00:57
-
-
Save adeishs/78a91e694f5aefd2840025e63848f4e3 to your computer and use it in GitHub Desktop.
Convert week number to Monday (ISO 8601)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH week AS ( | |
-- change week number and year as you need | |
SELECT CAST(2 AS INTEGER) AS number, | |
CAST(2022 AS INTEGER) AS year | |
), | |
first_date AS ( | |
SELECT CAST(year || '-01-01' AS DATE) AS d | |
FROM week | |
), | |
first_monday AS ( | |
SELECT (d + (8 - CAST(EXTRACT(DOW FROM d) AS INTEGER)) % 7) AS d | |
FROM first_date | |
) | |
SELECT first_monday.d + (week.number - 1) * 7 | |
FROM week | |
CROSS JOIN first_monday; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment