MySQL WEEKOFYEAR() Function

MySQL WEEKOFYEAR() function; Through this tutorial, i am going to show you MySQL WEEKOFYEAR() function with the help of examples.

MySQL WEEKOFYEAR() Function

WEEKOFYEAR() function in MySQL is used to find the week number for a given date. If the date is NULL, the WEEKOFYEAR function will return NULL. Otherwise, it returns the value of week which ranges between 1 to 53

Syntax of MySQL WEEKOFYEAR() Function

The WEEKOFYEAR() function syntax is:

WEEKOFYEAR(date)

Here date is the date value that you want return the WEEKOFYEAR name from.

Example 1 – MySQL WEEKOFYEAR() Function

Let’s take first simple example using mysql weekofyear() function; as follows:

SELECT WEEKOFYEAR('2019-07-11') AS 'Result';

Output-1

 +---------+
 | Result  |
 +---------+
 |   28    |
 +---------+

Example 2 – MySQL WEEKOFYEAR() Function

Let’s take an example for fetch a record / data from the mysql database table using WEEKOFYEAR() function; as follows:

 SELECT
 created_at AS create_date,
 WEEKOFYEAR(created_at) AS week_of_year
 FROM users
 WHERE id = 1;

Output-2

 +---------------------+--------------+
 | create_date         | week_of_year |
 +---------------------+--------------+
 | 2019-07-11 11:30:37 |       28     |
 +---------------------+--------------+

Example 3 – MySQL WEEKOFYEAR() with now() Function

Let’s take an example using current date with WEEKOFYEAR() function; as follows:

 SELECT 
 NOW(),
 WEEKOFYEAR(NOW());

Output-3

 +---------------------+-------------------+
 | NOW()               | WEEKOFYEAR(NOW()) |
 +---------------------+-------------------+
 | 2018-07-13 10:05:41 |   28              |
 +---------------------+-------------------+

Example 4 – MySQL WEEKOFYEAR() + CURDATE() Function

Let’s take an example using the WEEKOFYEAR() and CURDATE() function; as follows:

 SELECT 
CURDATE(),
WEEKOFYEAR(CURDATE());

Output-4

 +------------+-----------------------+
 | CURDATE()  | WEEKOFYEAR(CURDATE()) |
 +------------+-----------------------+
 | 2019-07-13 |     28                |
 +------------+-----------------------+

Conclusion

MySQL weekofyear() function tutorial, You have learned how to use WEEKOFYEAR() function with various examples.

Leave a Comment