Skip to main content
Version: Latest-3.3

yearweek

Description

Returns year and week number for a given date. This function works in the same way as the yearweek() function in MySQL.

This function is supported from v3.3 onwards.

Syntax

INT YEARWEEK(DATETIME|DATE date[, INT mode])

Parameters

  • date: The supported data types are DATETIME and DATE.
  • mode: optional. The supported data type is INT. This parameter is used to specify the logic for calculating the yearweek number, that is, whether the week starts on Sunday or Monday, and whether the return value is in the range of 053 or 153. Value range: 0~7. Default value: 0. If this parameter is not specified, mode 0 is used by default. The following table describes how this parameter works.
ModeFirst day of weekRangeWeek 1 is the first week …
0Sunday0-53with a Sunday in this year
1Monday0-53with 4 or more days this year
2Sunday1-53with a Sunday in this year
3Monday1-53with 4 or more days this year
4Sunday0-53with 4 or more days this year
5Monday0-53with a Monday in this year
6Sunday1-53with 4 or more days this year
7Monday1-53with a Monday in this year

Return value

Returns a value of the INT type. Value range: 0~53. The specific range is determined based on the mode parameter. NULL is returned if the value of date is invalid or the input value is empty.

Examples

Returns year and week for a date. The value of the mode argument defaults to 0

mysql> SELECT YEARWEEK('2007-01-01', 0);
+---------------------------+
| yearweek('2007-01-01', 0) |
+---------------------------+
| 200653 |
+---------------------------+
mysql> SELECT YEARWEEK('2007-01-01', 1);
+---------------------------+
| yearweek('2007-01-01', 1) |
+---------------------------+
| 200701 |
+---------------------------+
mysql> SELECT YEARWEEK('2007-01-01', 2);
+---------------------------+
| yearweek('2007-01-01', 2) |
+---------------------------+
| 200653 |
+---------------------------+
1 row in set (0.01 sec)