MySQL YEARWEEK() Function

The YEARWEEK() function in MySQL extracts the year and week number from a given date. This is helpful for tasks involving weekly data analysis or reporting.



YEARWEEK(): Definition and Usage

This function returns a single number representing both the year and the week number within that year. The week number ranges from 0 to 53. The way MySQL defines the "first week" of the year can be customized (explained below).

Syntax

Syntax

YEARWEEK(date, firstdayofweek)
      

Parameter Values

Parameter Description
date The date or datetime value from which you want to extract the year and week number. This is required.
firstdayofweek (Optional) Specifies the first day of the week. If omitted, it defaults to Sunday. Values:
0: Sunday
1: Monday (first week has > 3 days)
2: Sunday
3: Monday (first week has > 3 days)
4: Sunday (first week has > 3 days)
5: Monday
6: Sunday (first week has > 3 days)
7: Monday

Examples

Extracting Year and Week Number

This example extracts the year and week number for June 15th, 2017. The output will be in the format YYYYWW (e.g., 201724).

Syntax

SELECT YEARWEEK("2017-06-15");
      
Output

201724
      

Another Date Example

This shows another example, using October 25th, 2017.

Syntax

SELECT YEARWEEK("2017-10-25");
      
Output

201743
      

Using the Current Date

This extracts the year and week number for today's date.

Syntax

SELECT YEARWEEK(CURDATE());
      
Output

(The output will be the current year and week number.)  e.g., 202435