MySQL WEEKOFYEAR function is a Date Function, which is used to return the Calendar week number from the given date. This function returns value range from 1 to 53.
In this article, we will show you, How to use this WEEKOFYEAR function to get the Week of Year in MySQL from an expression with example.
MySQL WEEKOFYEAR Syntax
The basic syntax of the WEEKOFYEAR() Function in MySQL is as shown below:
WEEKOFYEAR(date or expression);
TIP: This MySQL WEEKOFYEAR function is equivalent to WEEK(date, 3).
MySQL WEEKOFYEAR function Example
The following queries helps you understand the basic use of Week of Year function. Here, we are returning the Calendar Week number from the date expression, and the Date and time expression.
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 current date returned by CURDATE() function.
MySQL WEEKOFYEAR Function Example 2
The following query will show you, 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 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 date.
Thank You for Visiting Our Blog