MySQL - WEEK() Function



The MYSQL WEEK() function is used to retrieve the calendar week of the given date. This function returns a numerical value, ranging from 0 to 53 or 1 to 53.

In addition to the date value this function accepts another optional parameter representing the mode using which you can specify whether the week should start on Sunday or Monday.

Following are the various possible values of the mode and the respective implications −

ModeFirst day of WeekWeek values RangeFirst week of year has...
0Sunday0 to 53Sunday as the first day
1Monday0 to 53More than 3 days
2Sunday1 to 53Sunday as the first day
3Monday1 to 53More than 3 days
4Sunday0 to 53More than 3 days
5Monday0 to 53Monday as the first day
6Sunday1 to 53More than 3 days
7Monday1 to 53Monday as the first day

Syntax

Following is the syntax of MySQL WEEK() function −

WEEK(date);

Parameters

This method accepts the date from which we need to extract the week number as a parameter.

Return value

This function returns an integer representing the week number for the specified date. The week number can range from 1 to 53, depending on the year and the mode used.

Example

In the following example, we are using the MySQL WEEK() function to retrieve the week number for the specified date −

SELECT WEEK('2022-11-20') As Result;

Output

This will produce the following result −

Result
47

Example

The below query will return the week of the specified date (2022-11-20). 1 (Mode) as the second argument defines that the first day of the week is assumed as Monday −

SELECT WEEK('2022-11-20', 1) As Result;

Output

Following is the output −

Result
739575

Example

Here, we are retrieving the week number of the current date −

SELECT WEEK(CURDATE()) As Result;

Output

Following is the output −

Result
47

Example

In the following example, let us create a table named ORDERS using CREATE TABLE statement −

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

Now, insert the following records into the ORDERS table using the INSERT statement −

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

Execute the below query to fetch all the inserted records in the above-created table −

Select * From ORDERS;

Following is the ORDERS table −

OIDDATECUSTOMER_IDAMOUNT
1022009-10-08 00:00:0033000.00
1002009-10-08 00:00:0031500.00
1012009-11-20 00:00:0021560.00
1032008-05-20 00:00:0042060.00

Now, we use the MySQL WEEK() function to calculate the week number for each date value in the "DATE" column.

Result1 is the week number based on the default mode, and Result2 is based on mode 1, considering Monday as the first day of the week.

Select OID, DATE, WEEK(DATE) As Result1, WEEK(DATE, 1) As Result2
From ORDERS;

Output

The output is displayed as follows −

OIDDATEResult1Result2
1022009-10-08 00:00:004041
1002009-10-08 00:00:004041
1012009-11-20 00:00:004647
1032008-05-20 00:00:002021