MySQL WEEKOFYEAR() Function
The WEEKOFYEAR()
function in MySQL extracts the week number from a date value. This is particularly useful for tasks related to weekly reporting or data analysis.
WEEKOFYEAR(): Definition and Usage
WEEKOFYEAR()
returns an integer representing the week number within the year. The week numbers range from 1 to 53. It assumes that the first week of the year is the first week that has at least 4 days in the new year, and that Monday is the first day of the week.
Syntax
Syntax
WEEKOFYEAR(date)
Parameter Values
Parameter | Description |
---|---|
date |
The date or datetime value. This is required. |
Related Function
The WEEK()
function offers more flexibility in defining the first week of the year and the first day of the week.
Examples
Week Number for a Specific Date
This example shows how to get the week number for June 15th, 2017.
Syntax
SELECT WEEKOFYEAR("2017-06-15");
Output
24
Week Number for January 1st
This illustrates that January 1st might not always be in week 1, depending on the day of the week.
Syntax
SELECT WEEKOFYEAR("2017-01-01");
Output
52
Week Number for the Current Date
This gets the current week number.
Syntax
SELECT WEEKOFYEAR(CURDATE());
Output
(The current week number, 1-53)