MySQL WEEKOFYEAR is one of the Date Functions, which returns the Calendar week number from the given date. This function returns value range from 1 to 53.
Let us see how to use this WEEKOFYEAR function to get the Week of Year 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-07-25 09:14:27');
SELECT WEEKOFYEAR('2018-10-25 09:14:27');
Let us see another example of the 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().
The following MySQL WEEKOFYEAR query shows 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);
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.