MySQL WEEKOFYEAR() function; In this tutorial, we will learn how to MySQL WEEKOFYEAR() function with the help of examples.
And as well as, 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 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.
Examples of MySQL WEEKOFYEAR() Function
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.
Recommended MySQL Tutorials
If you have any questions or thoughts to share, use the comment form below to reach us.