MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL WEEK() Function



The MySQL WEEK() function returns week portion of a given date or datetime value. It can be between 0-53 or 1-53 depending on the specified mode.

The WEEKOFYEAR() function returns the same as the WEEK() function with the syntax of WEEK(datetime, 3).

Syntax

WEEK(datetime, mode)

Parameters

datetime Required. Specify a date or datetime value from which to extract the week.
mode Optional. Specify what day the week starts on. It can take value between 0-7. See the table below for description:

mode and description

modeDescriptionReturns
0First day of the week is Sunday0-53
1First day of the week is Monday and the first week has more than 3 days0-53
2First day of the week is Sunday1-53
3First day of the week is Monday and the first week has more than 3 days1-53
4First day of the week is Sunday and the first week has more than 3 days0-53
5First day of the week is Monday0-53
6First day of the week is Sunday and the first week has more than 3 days1-53
7First day of the week is Monday1-53

Note: If mode argument is omitted, the value of default_week_format system variable is used.

Return Value

Returns the week portion of a given date or datetime value.

Example 1:

The example below shows the usage of WEEK() function.

mysql> SELECT WEEK('2018-08-18');
Result: 32

mysql> SELECT WEEK('2018-08-18 10:38:42');
Result: 32

mysql> SELECT WEEK('2018-08-18 10:38:42.000004');
Result: 32

mysql> SELECT WEEK('2014-10-25');
Result: 42

mysql> SELECT WEEK(CURDATE());
Result: 48

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the week portion of records of column OrderTime:

SELECT *, WEEK(OrderTime, 5) AS WEEK_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeWEEK_Value
1001.582017-08-18 10:38:4233
1201.612018-03-23 07:14:1612
1251.782018-09-12 05:25:5637
501.802019-01-16 11:52:052
2001.722020-02-06 09:31:345

❮ MySQL Functions