WEEKOFYEAR() Function Examples – MySQL

In this MySQL tutorial, you will learn how MySQL WEEKOFYEAR() function. We will provide many examples of mysql WEEKOFYEAR() with it’s syntax.

We will also explain the difference between Mysql WEEK() vs WEEKOFYEAR() with example.

MySQL WEEKOFYEAR() Function

In MySQL, WEEKOFYEAR function is used to returns the calendar week for a given date . Just provide the date as a argument(params) and the it will return the calendar week .

Syntax

The WEEKOFYEAR() function syntax is:

WEEKOFYEAR(date)

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

Examples

Now we take different different type of examples of WEEKOFYEAR function. See below all the examples one by one.

Example-1

First simple example to demonstrate.

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

Output-1

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

Example-2 With Database Queries

For some time we want to fetch a record / data from the mysql database table. When we need to get the week of year of a table in the database. In that case we use WEEKOFYEAR() with mysql queries.

Next, we take example-2, in this example we will fetch WEEKOFYEAR of database table name users where column name is created_at.

 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 of WEEK vs WEEKOFYEAR()

In example-3, we take an example to differentiate WEEKOFYEAR() and WEEK() function.

 SET @date = '2019-07-13';
 SELECT 
   DAYOFWEEK(@date) AS 'WEEK',
   WEEKOFYEAR(@date) AS 'WEEKOFYEAR';
WEEK()WEEKOFYEAR()
The WEEK() function returns the week number for a given date (a number from 0 to 53). The WEEKOFYEAR() function returns the week number for a given date (a number from 1 to 53).

Output-3

+-------------+--------------+
| WEEK        | WEEKOFYEAR   |
+-------------+--------------+
| 7           |       28     |
+-------------+--------------+

Example-4 Using Current Date/Time

Now we take new example using current date with WEEKOFYEAR() function. It will return week of year from the current date & time.

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

Output-4

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

Example-5 Using MySQL CURDATE() Function

Next, we will take another example of WEEKOFYEAR() with CURDATE() function. Which is returns only the week of year.

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

Output-5

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

Conclusion

Here, You have learned how to use WEEKOFYEAR() function with various examples.

If you have any questions or thoughts to share, use the comment form below to reach us.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *