// dates & times reference

SQL YEARWEEK function

Returns a combined year-week integer where the year follows the week, so December 30 can land in 202501 under ISO mode 3.

Categoria
Dates & times
Returns
integer
In this category
48 of 121

Sintaxe

YEARWEEK(d)  -- mode 0
YEARWEEK(d, 3)  -- ISO week
TO_CHAR(d, 'IYYYIW')  -- PostgreSQL equivalent

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Different name or syntax DATEPART(iso_week, d) plus a computed ISO year
PostgreSQL Different name or syntax to_char(d, 'IYYYIW')
MySQL Sim Supported under this name.
SQLite Different name or syntax strftime('%G%V', d) on SQLite 3.46+
Oracle Different name or syntax TO_CHAR(d, 'IYYYIW')

Exemplos

MySQL

SELECT YEARWEEK('2024-12-30', 3) AS iso_yw;  -- 202501

MySQL

SELECT YEARWEEK(created_at, 3) AS wk, COUNT(*) FROM orders GROUP BY wk;

PostgreSQL

SELECT TO_CHAR(d, 'IYYYIW') AS iso_yw FROM events;

Bom saber

  • Without the year, week grouping merges across years: GROUP BY WEEK(d) pools week 3 of every year; YEARWEEK(d, 3) keeps years separate.
  • Mode 3 is ISO: weeks start Monday and week 1 holds the first Thursday. The default mode 0 starts weeks on Sunday, so YEARWEEK('2024-12-30') is 202452.
  • Before SQLite 3.46 there is no ISO week code: combining %Y with %W gives 2024 for 2024-12-30 where ISO says 2025. Use %G%V on 3.46+.

Questions about YEARWEEK

Why does YEARWEEK of 2024-12-30 return 202501?

Under ISO rules that date falls in the first week of 2025: the year follows the week, which is exactly the point of the function.

What is the SQL Server equivalent?

There is no iso_year datepart. YEAR(DATEADD(day, 26 - DATEPART(iso_week, d), d)) * 100 + DATEPART(iso_week, d) reproduces YEARWEEK(d, 3).

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.