MySQL YEARWEEK() Function; Through this tutorial, i am going to show you how to use of MySQL YEARWEEK() function with the help of examples.
MySQL YEARWEEK() Function
YEARWEEK() function in MySQL is used to find year and week for a given date. If the date is NULL, the YEARWEEK() function will return NULL. Otherwise, it returns value of year which range from 1000 to 9999 and value of week which ranges between 0 to 53.
Syntax of MySQL YEARWEEK() Function
The basic syntax of mysql yearweek() function; as follows:
YEARWEEK(date) YEARWEEK(date,mode)
Where:
- The date is the date you want the number of weeks and weeks to come back from you.
- Mode is a number that specifies whether the week should start on Sunday or Monday, and weeks should be 53 or 1 to 53. See the table below for possible mode values.
If you do not specify the mode, the mode is 0.
Example 1 – MySQL YEARWEEK() Function
Let’s take first example for mysql yearweek() function; as follows:
SELECT YEARWEEK('2025-01-02') As 'Result';
Output-1
+--------+ | Result | +--------+ | 202452 | +--------+
Example 2 – MySQL YEARWEEK() Function
Let’s take second example of mysql yearweek() function; as follows:
SELECT YEARWEEK('1975-10-15') As 'Result';
Result:
+--------+ | Result | +--------+ | 197541 | +--------+
Example 3 – MySQL YEARWEEK() Function
However, you also have the option to present a second argument that you use the mode.
SELECT YEARWEEK('2020-12-10', 5) AS 'Mode 5';
Result:
+--------+ | Mode 5 | +--------+ | 202049 | +--------+
Below we are providing the list of mode values.
Mode | First day of week | Range | Week 1 is the first week … |
---|---|---|---|
0 | Sunday | 0-53 | with a Sunday in this year |
1 | Monday | 0-53 | with 4 or more days this year |
2 | Sunday | 1-53 | with a Sunday in this year |
3 | Monday | 1-53 | with 4 or more days this year |
4 | Sunday | 0-53 | with 4 or more days this year |
5 | Monday | 0-53 | with a Monday in this year |
6 | Sunday | 1-53 | with 4 or more days this year |
7 | Monday | 1-53 | with a Monday in this year |
These are the same values that can be used with the WEEK () function.
Example 4 – MySQL YEARWEEK() with Now() Function
To extract part of the year and week from the current date and time (which is now returned using the () function); as follows:
SELECT NOW(), WEEK(NOW());
Output-4
+---------------------+--------------------+ | NOW() | YEARWEEK(NOW()) | +---------------------+--------------------+ | 2019-07-10 18:30:44 | 201927 | +---------------------+--------------------+
Example 5 – MySQL YEARWEEK() + CURDATE() Function
Let’s take an example using curdate() and yearweek() function; as follows:
SELECT CURDATE(), YEARWEEK(CURDATE());
Output-5
+------------+-----------------------+ | CURDATE() | YEARWEEK(CURDATE()) | +------------+-----------------------+ | 2019-05-15 | 201927 | +------------+-----------------------+
Take a Look More Examples
Let’s take some MySQL YEARWEEK function examples and find out how to use the YEARWEEK function in MySQL.
mysql> SELECT YEARWEEK('2014-01-01'); Result: 201352 mysql> SELECT YEARWEEK('2014-01-05'); Result: 201401 mysql> SELECT YEARWEEK('2014-01-12'); Result: 201402 mysql> SELECT YEARWEEK('2014-07-16'); Result: 201428 mysql> SELECT YEARWEEK('2014-12-31'); Result: 201452 mysql> SELECT YEARWEEK('2015-01-01'); Result: 201452
Conclusion
MySQL YEARWEEK() Function tutorial, You have learned how to use mysql MONTH() function with various examples.