MySQL WEEKOFYEAR

MySQL WEEKOFYEAR is one of the Date Functions, which returns the Calendar week number from the given date. This MySQL WEEKOFYEAR function returns value range from 1 to 53.

Let us see how to use this WEEKOFYEAR function to get the Week of Year in MySQL from an expression with an example. The basic syntax of the MySQL WEEKOFYEAR Function is as shown below:

WEEKOFYEAR(date or expression);

MySQL WEEKOFYEAR function Example

The following queries help you understand the basic use of the Week of Year function. Here, we are returning the Calendar Week number from the date expression, and the Datetime expression.

TIP: This WEEKOFYEAR method is equivalent to WEEK(date, 3).

SELECT WEEKOFYEAR('2018-12-25');

SELECT WEEKOFYEAR('2018-07-25 09:14:27');

SELECT WEEKOFYEAR('2018-10-25 09:14:27');
MySQL WEEKOFYEAR Function 2

Let us see another example of MySQL WEEK OF YEAR function. Here, we are returning the Week number from the current datetime returned by the Now(), and the current date returned by CURDATE().

SELECT WEEKOFYEAR(NOW());

SELECT WEEKOFYEAR(CURDATE());
MySQL WEEKOFYEAR Function 3

WEEKOFYEAR Example 2

The following query show what happens when we try to find the calendar week number from a date in string format or the invalid DATETIME.

SELECT WEEKOFYEAR(CURDATE() + 120);

SELECT WEEKOFYEAR(0);

SELECT WEEKOFYEAR('2012-22-22');
MySQL WEEKOFYEAR Function 4

From the above screenshot, (0) means we are trying to extract a week number from a zero date part. That’s why it returns NULL. Within the Last statement, (‘2012-22-22’); is returning NULL. Because it is an invalid MySQL dt.