SQL week numbers per engine
Week numbering has four competing conventions (ISO 8601, Sunday-first, Monday-first and calendar-anchored), and each engine exports a different subset.
- Categoría
- Dates & times
- Returns
integer- In this category
- 104 of 121
Sintaxis
to_char(d, 'IW')
WEEK(d, mode)
DATEPART(ISO_WEEK, d)
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Different name or syntax | DATEPART(ISO_WEEK, d); plain WEEK follows DATEFIRST |
| PostgreSQL | Different name or syntax | to_char(d, 'IW') ISO; EXTRACT(WEEK) is also ISO |
| MySQL | Sí | WEEK(d, mode) / YEARWEEK with modes 0-7 |
| SQLite | Different name or syntax | strftime %W (Monday weeks); %U (Sunday weeks) and ISO %V since 3.46 |
| Oracle | Different name or syntax | TO_CHAR(d, 'IW') ISO; 'WW' is Jan-1-anchored |
Ejemplos
PostgreSQL
SELECT to_char(DATE '2026-01-01', 'IW'); -- 01 (ISO: belongs to 2026-W01)
MySQL
SELECT WEEK('2026-01-01', 3), YEARWEEK('2026-01-01', 3); -- 1, 202601
SQL Server
SELECT DATEPART(ISO_WEEK, '2026-01-01'); -- 1
Conviene saberlo
- ISO 8601 ('IW', WEEK mode 3, ISO_WEEK): weeks start Monday, week 1 contains the first Thursday, and year boundaries roll weeks into the adjacent ISO year; Dec 29 2025 is ISO week 1 of 2026.
- MySQL's WEEK has 8 modes (0-7) mixing Sunday/Monday start and week-1 definitions; YEARWEEK(d, 3) pairs with %x-%v in DATE_FORMAT for ISO year-week keys.
- SQL Server's plain WEEK depends on SET DATEFIRST and counts weeks from Jan 1. Never use it for interchange; ISO_WEEK exists for that.
Questions about Week numbers
Why do two engines give different week numbers for the same date?
Different conventions: ISO weeks vs Jan-1-anchored weeks vs DATEFIRST-dependent counts. Agree on ISO 8601 and use IW / ISO_WEEK / WEEK(d,3) everywhere it exists.
How do I group by year-week in MySQL?
DATE_FORMAT(d, '%x-%v') with ISO week numbers, or YEARWEEK(d, 3) as a sortable integer; %U and %W follow the non-ISO modes.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.