MySQL YEARWEEK() Function Examples – MySQL
Mysql 01-Jun-2021

MySQL YEARWEEK() Function Examples – MySQL

In this MySQL Tutorial, we would love to share with you how to use of MySQL YEARWEEK() function with different examples.

MySQL YEARWEEK() Function

The MySQL YEARWEEK() function is used to returns the year and week for a given date. If you pass the date into the YEAR WEEK function, it will return the result.

You also have the option of specifying whether the week is to start on Sunday or Monday and should be in the range of 0 to 53 or 1 to 53, or not.

Syntax

Here is YEARWEEK() function syntax:

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

Now we take an example to demonstrate.

SELECT YEARWEEK('2025-01-02') As 'Result';

Output-1

+--------+
| Result |
+--------+
| 202452 |
+--------+

Example-2

Next, we take different date example.

SELECT YEARWEEK('1975-10-15') As 'Result';

Result:

+--------+
| Result |
+--------+
| 197541 |
+--------+

Example-3 with Specify a Mode

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 | Current Date/Time

Let’s take another example , extracting part of the year and week from the current date and time (which is now returned using the () function).

  SELECT 
  NOW(),
  WEEK(NOW());

Output-4

+---------------------+--------------------+
| NOW()               | YEARWEEK(NOW())    |
+---------------------+--------------------+
| 2019-07-10 18:30:44 |          201927    |
+---------------------+--------------------+

Example-5 | CURDATE() Function

Let’s take a another example of using CURDATE() function. Basically CURDATE() function returns only the date without time. Here it will return the year and week.

SELECT 
CURDATE(),
YEARWEEK(CURDATE());    

Output-5

+------------+-----------------------+
| CURDATE()  | YEARWEEK(CURDATE())   |
+------------+-----------------------+
| 2019-05-15 |            201927     |
+------------+-----------------------+

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

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