MySQL Tutorial MySQL Advanced MySQL Database Account Management MySQL References

MySQL WEEKOFYEAR() Function



The MySQL WEEKOFYEAR() function returns week of the year of a given date or datetime value. It can be between 1 to 53.

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

Note: The WEEKOFYEAR() function assumes that the first day of the week is Monday and the first week has more than 3 days.

Syntax

WEEKOFYEAR(datetime)

Parameters

datetime Required. Specify a date or datetime value from which to extract the week of the year.

Return Value

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

Example 1:

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

mysql> SELECT WEEKOFYEAR('2018-08-18');
Result: 33

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

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

mysql> SELECT WEEKOFYEAR('2014-10-25');
Result: 43

mysql> SELECT WEEKOFYEAR(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 of the year of records of column OrderTime:

SELECT *, WEEKOFYEAR(OrderTime) AS WEEKOFYEAR_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeWEEKOFYEAR_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:053
2001.722020-02-06 09:31:346

❮ MySQL Functions