In MySQL, you can use the WEEKDAY() function to return the weekday index for a given date. Simply provide the date as an argument and the function will return the weekday index.
Syntax
The syntax goes like this:
WEEKDAY(date)
Where date is the date you want the weekday returned from.
Example 1 – Basic Usage
Here’s an example to demonstrate.
SELECT WEEKDAY('2021-01-21') AS 'Weekday';
Result:
+---------+ | Weekday | +---------+ | 3 | +---------+
Example 2 – Comparison with DAYNAME()
To make it easier for us to verify the results, here I run both the WEEKDAY() and DAYNAME() functions side by side.
SET @date = '2021-01-21'; SELECT DAYNAME(@date) AS 'Day Name', WEEKDAY(@date) AS 'Weekday';
Result:
+----------+---------+ | Day Name | Weekday | +----------+---------+ | Thursday | 3 | +----------+---------+
Weekday Index
If you’re wondering why Thursday is only the 3rd day of the week, here’s how the weekday index is calculated:
| Index | Week Day |
|---|---|
| 0 | Monday |
| 1 | Tuesday |
| 2 | Wednesday |
| 3 | Thursday |
| 4 | Friday |
| 5 | Saturday |
| 6 | Sunday |