MySQL DAYOFWEEK

MySQL DAYOFWEEK is one of the Date Functions used to return the weekday index value of the given date. This method returns values from 1 to 7 where 1 = Sunday, 2 = Monday, ….. 7 = Sunday.

Let us see how to use this MySQL DAYOFWEEK function to get the weekday or Day of Week from an expression with an example. The basic syntax of the DAYOFWEEK() Function is as shown below:

DAYOFWEEK(date or expression);

MySQL DAYOFWEEK function Example

These examples help you understand the use of the dayofweek method. Here, we are returning the Weekday index number from the date expression and the DateTime expression.

SELECT DAYOFWEEK('2018-12-30');

SELECT DAYOFWEEK('2018-12-31');

SELECT DAYOFWEEK('2018-12-15 03:22:19');
MySQL DAYOFWEEK Example 1

How to get the Day of the Week From the Current Date?

Let us see another example of the MySQL DAYOFWEEK function. Here, we are returning the Weekday index number from the current DateTime returned by the Now() and CURDATE().

SELECT DAYOFWEEK(NOW()), DAYOFWEEK(CURRENT_DATE());
DAY OF WEEK Example 2

Use DAYOFWEEK on String and Invalid Dates

The following MySQL queries show what happens when we try to return the weekday index from 0 format, string format, or invalid dates.

SELECT DAYOFWEEK(NOW() + 2);

SELECT DAYOFWEEK('2019-02-31');

SELECT DAYOFWEEK('0000-00-00');
MySQL DAYOFWEEK Function 3

From the above screenshot (‘2019-02-31’); the statement returned NULL because it’s an invalid dt. Within the Last Date Function statement, (0) means we are trying to extract the day of the week from a zero date part. That’s why it returns NULL.