Skip to content

Instantly share code, notes, and snippets.

@janispritzkau
Created August 25, 2022 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janispritzkau/35b54c4c9193b94eb0ae16b1251c0fa7 to your computer and use it in GitHub Desktop.
Save janispritzkau/35b54c4c9193b94eb0ae16b1251c0fa7 to your computer and use it in GitHub Desktop.
Start date and week count for first calendar week of year (PostgreSQL)
SELECT
year,
date_trunc('week', make_date(year, 1, 4))::date AS start, -- first calendar week always contains January 4th.
extract(week from make_date(year + 1, 1, 4) - 7) AS weeks
FROM generate_series(2000, 2030) t(year);
year | start | weeks
------+------------+-------
2000 | 2000-01-03 | 52
2001 | 2001-01-01 | 52
2002 | 2001-12-31 | 52
2003 | 2002-12-30 | 52
2004 | 2003-12-29 | 53
2005 | 2005-01-03 | 52
2006 | 2006-01-02 | 52
2007 | 2007-01-01 | 52
2008 | 2007-12-31 | 52
2009 | 2008-12-29 | 53
2010 | 2010-01-04 | 52
2011 | 2011-01-03 | 52
2012 | 2012-01-02 | 52
2013 | 2012-12-31 | 52
2014 | 2013-12-30 | 52
2015 | 2014-12-29 | 53
2016 | 2016-01-04 | 52
2017 | 2017-01-02 | 52
2018 | 2018-01-01 | 52
2019 | 2018-12-31 | 52
2020 | 2019-12-30 | 53
2021 | 2021-01-04 | 52
2022 | 2022-01-03 | 52
2023 | 2023-01-02 | 52
2024 | 2024-01-01 | 52
2025 | 2024-12-30 | 52
2026 | 2025-12-29 | 53
2027 | 2027-01-04 | 52
2028 | 2028-01-03 | 52
2029 | 2029-01-01 | 52
2030 | 2029-12-31 | 52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment