MySQL DAYOFWEEK() Function
The DAYOFWEEK()
function in MySQL extracts the day of the week from a date, returning a numerical index that represents the day. This is helpful for tasks involving weekly data analysis or scheduling.
DAYOFWEEK(): Definition and Usage
DAYOFWEEK()
takes a date or datetime value and returns an integer from 1 to 7, where 1 represents Sunday, 2 represents Monday, and so on until 7, which represents Saturday. This numbering system is convenient for various weekly data processing and reporting tasks.
Syntax
Syntax
DAYOFWEEK(date)
Parameter Values
Parameter | Description |
---|---|
date |
The date or datetime value. This is required. |
Examples
Weekday Index for a Specific Date
This example gets the weekday index for June 15th, 2017.
Syntax
SELECT DAYOFWEEK("2017-06-15");
Output
5 (Thursday, since 1=Sunday, 2=Monday... 7=Saturday)
Weekday Index for a DateTime Value
This example shows that DAYOFWEEK()
works with both date and datetime values, ignoring the time component.
Syntax
SELECT DAYOFWEEK("2017-06-15 09:34:21");
Output
5
Weekday Index for Today
This gets the weekday index for the current date.
Syntax
SELECT DAYOFWEEK(CURDATE());
Output
(The weekday index for today, a number between 1 and 7)