MySQL WEEKOFYEAR function is one of the MySQL 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 WEEKOFYEAR() Function in MySQL 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 Date and time expression.
TIP: This WEEKOFYEAR Date Function is equivalent to WEEK(date, 3).
SELECT WEEKOFYEAR('2018-07-25 09:14:27');
SELECT WEEKOFYEAR('2018-10-25 09:14:27');
Let us see another example of MySQL WEEK OF YEAR function. Here, we are returning the Week number from the current date and time returned by the Now() function, and the current date returned by CURDATE() function.
MySQL WEEKOFYEAR Function Example 2
The following query show what happens when we try to find the calendar week number from a date in string format or invalid dates.
SELECT WEEKOFYEAR(CURDATE() + 120);
From the above screenshot, WEEKOFYEAR(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, WEEKOFYEAR(‘2012-22-22’); is returning NULL. Because it is an invalid MySQL date.