MySQL YEARWEEK() Function Example

MySQL YEARWEEK() Function Example

MySQL YEARWEEK() Function; In this tutorial, we would love to share with you how to use of MySQL YEARWEEK() function with the help of 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

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.

ModeFirst day of weekRangeWeek 1 is the first week …
0Sunday0-53with a Sunday in this year
1Monday0-53with 4 or more days this year
2Sunday1-53with a Sunday in this year
3Monday1-53with 4 or more days this year
4Sunday0-53with 4 or more days this year
5Monday0-53with a Monday in this year
6Sunday1-53with 4 or more days this year
7Monday1-53with a Monday in this year

These are the same values ​​that can be used with the WEEK () function.

Example-4

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

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.

Recommended MySQL Tutorials

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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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