MySQL DAYOFWEEK Function
The DAYOFWEEK
function returns the weekday index for a date i.e., 1 for Sunday, 2 for Monday, … 7 for Saturday. These index values correspond to the ODBC standard.
The following illustrates the DAYOFWEEK
function:
DAYOFWEEK(date)
Code language: SQL (Structured Query Language) (sql)
The DAYOFWEEK
function accepts 1 argument which is a DATE
or DATETIME
value. It returns an integer which ranges from 1 to 7 that represents Sunday to Saturday.
The DAYOFWEEK
function returns NULL
if the date is NULL
, zero ( 0000-00-00
), or invalid.
The following example returns weekday index of December 1st, 2010
mysql> SELECT DAYNAME('2012-12-01'), DAYOFWEEK('2012-12-01');
+-----------------------+-------------------------+
| DAYNAME('2012-12-01') | DAYOFWEEK('2012-12-01') |
+-----------------------+-------------------------+
| Saturday | 7 |
+-----------------------+-------------------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
In this query, the DAYNAME
function returns weekday’s name while DAYOFWEEK
function returns the weekday index of December 1st, 2011
.