VeloDB Cloud
SQL Manual
Functions
date-time-functions
yearweek

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.

ModeFirst day of weekRangeDefinition of the first week
0Sunday1-53The week containing the first Sunday of the year
1Monday1-53The first week in the year that no less than 4 days of the week belongs to the year
2Sunday1-53The week containing the first Sunday of the year
3Monday1-53The first week in the year that no less than 4 days of the week belongs to the year
4Sunday1-53The first week in the year that no less than 4 days of the week belongs to the year
5Monday1-53The week containing the first Monday of the year
6Sunday1-53The first week in the year that no less than 4 days of the week belongs to the year
7Monday1-53The 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