← Back to SQL, page 9

sql-0161 · Medium mid

How do you get the ISO week number of a date, and why can the first days of January belong to week 52 or 53?

ISO weeks start on Monday, and week 1 is the week containing the year's first Thursday. So 1 January 2027, a Friday, belongs to ISO week 53 of 2026. Use strftime('%V') with '%G' for the ISO year in SQLite 3.46+, or EXTRACT(WEEK) and EXTRACT(ISOYEAR) in PostgreSQL. SQLite's older '%W' uses a different rule.

Full answer
sql
SELECT strftime('%G-W%V', '2027-01-01') AS iso_week, strftime('%Y-%W', '2027-01-01') AS pct_w,
       strftime('%G-W%V', '2027-01-04') AS first_monday;
iso_weekpct_wfirst_monday
2026-W532027-002027-W01

%V is the ISO week and %G the ISO year that the week belongs to. 1 January 2027 falls in week 53 of ISO year 2026, so grouping by calendar year plus ISO week would put it in the wrong bucket. Pair %V with %G, never with %Y.

%W counts weeks starting on the first Monday of the calendar year, with earlier days in week 00, which is a different scheme.

PostgreSQL:

sql
SELECT EXTRACT(ISOYEAR FROM DATE '2027-01-01') AS iso_year, EXTRACT(WEEK FROM DATE '2027-01-01') AS iso_week;
iso_yeariso_week
202653
sql
SELECT strftime('%G-W%V', '2027-01-01') AS iso_week, strftime('%Y-%W', '2027-01-01') AS pct_w,
       strftime('%G-W%V', '2027-01-04') AS first_monday;

Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.

Key points

  • ISO weeks start Monday; week 1 contains the first Thursday
  • Early January can be in week 52 or 53 of the previous ISO year
  • Pair ISO week with ISO year (%G or ISOYEAR)
  • %W is not the ISO week

Common mistakes

  • Combining calendar year with ISO week
  • Using %W and calling it the ISO week

Follow-ups

  • How would you group sales by ISO week?
  • Which countries use Sunday-start weeks in reporting?

Tips

  • Give the 1 January example