yearweek
Description
Syntax:
INT YEARWEEK(DATE date)
INT YEARWEEK(DATE date, INT mode)
This function returns the year and week of a given date.The default value of mode
is 0.
If the week of the date belongs to the previous year, it will return the year and week number of the previous year; If the week of the date belongs to the next year, it will return the year number of the next year and the week number as 1.
The following table describes how the mode
argument works.
Mode | First day of week | Range | Definition of the first week |
---|---|---|---|
0 | Sunday | 1-53 | The week containing the first Sunday of the year |
1 | Monday | 1-53 | The first week in the year that no less than 4 days of the week belongs to the year |
2 | Sunday | 1-53 | The week containing the first Sunday of the year |
3 | Monday | 1-53 | The first week in the year that no less than 4 days of the week belongs to the year |
4 | Sunday | 1-53 | The first week in the year that no less than 4 days of the week belongs to the year |
5 | Monday | 1-53 | The week containing the first Monday of the year |
6 | Sunday | 1-53 | The first week in the year that no less than 4 days of the week belongs to the year |
7 | Monday | 1-53 | The week containing the first Monday of the year |
The parameter is of DATE or DATETIME type.
Example
mysql> select yearweek('2021-1-1');
+----------------------+
| yearweek('2021-1-1') |
+----------------------+
| 202052 |
+----------------------+
mysql> select yearweek('2020-7-1');
+----------------------+
| yearweek('2020-7-1') |
+----------------------+
| 202026 |
+----------------------+
mysql> select yearweek('2024-12-30',1);
+------------------------------------+
| yearweek('2024-12-30 00:00:00', 1) |
+------------------------------------+
| 202501 |
+------------------------------------+
Keywords
YEARWEEK