Function Reference

WEEK

PostgreSQL equivalent:

Returns the week number within the year of a date.

MySQL 8.1
WEEK(date[, mode])
  • [ ]optional part
date
Date or datetime
DATE
mode
Week numbering mode; taken from the default_week_format variable by default, usually 0
INTOptional
01234567

Return value

An integer from 0 to 53 or from 1 to 53 depending on mode; NULL for an incomplete date and for an unrecognized value

Examples

Week number by default

MySQL 8.1
SELECT WEEK('2022-06-16');
WEEK('2022-06-16')
24

January 1 in different modes

MySQL 8.1
SELECT WEEK('2022-01-01') AS mode_0,
	WEEK('2022-01-01', 3) AS mode_3;
mode_0mode_3
052

Details

Days before the first week of the year get number 0 in modes 0, 1, 4 and 5, and the number of the last week of the previous year in modes 2, 3, 6 and 7: WEEK('2022-01-01', 0) returns 0, and WEEK('2022-01-01', 2) returns 52.

It also works the other way: in modes 3 and 6, the last days of December can fall into the first week of the next year: WEEK('2024-12-30', 3) returns 1. To see the year of the week too, use YEARWEEK.

modeFirst day of the weekRangeWeek 1 is the first week that
0Sunday0–53has a Sunday
1Monday0–53has 4 or more days of this year
2Sunday1–53has a Sunday
3Monday1–53has 4 or more days of this year (ISO 8601)
4Sunday0–53has 4 or more days of this year
5Monday0–53has a Monday
6Sunday1–53has 4 or more days of this year
7Monday1–53has a Monday

See also