MySQL WEEKDAY Function
The WEEKDAY
function returns a weekday index for a date i.e., 0 for Monday, 1 for Tuesday, … 6 for Sunday.
The following illustrates the WEEKDAY
function:
WEEKDAY(date)
Code language: SQL (Structured Query Language) (sql)
The WEEKDAY
function accepts 1 argument which is a DATE
or DATETIME
value. It returns an integer which ranges from 0 to 6 that represents Monday to Sunday.
The WEEKDAY
function returns NULL
if the date is NULL
, invalid or zero ( 0000-00-00
).
See the following example:
mysql> SELECT DAYNAME('2010-01-01'), WEEKDAY('2010-01-01');
+-----------------------+-----------------------+
| DAYNAME('2010-01-01') | WEEKDAY('2010-01-01') |
+-----------------------+-----------------------+
| Friday | 4 |
+-----------------------+-----------------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
In this example, we used the DAYNAME
function to get the weekday’s name and WEEKDAY
function to get the weekday index of January 1st, 2010
.