What is DAYOFWEEK() in SQL?

The DAYOFWEEK() function returns the weekday index from a date sent as a parameter.

Figure 1 shows a visual representation of the DAYOFWEEK() function and the week index.

Figure 1: Visual representation of the DAYOFWEEK() function and week index

Syntax

DAYOFWEEK(date)

Parameter

The DAYOFWEEK() function takes the date as a parameter.

The date must be in the format YYYY-MM-DD, or else this function returns NULL.

Return value

The DAYOFWEEK() function returns the week index from a date sent as a parameter.

It returns NULL if the date is invalid.

Code

-- current date
SELECT DAYOFWEEK(NOW());
-- invalid month
SELECT DAYOFWEEK('2021-0-12');
-- valid month/day
SELECT DAYOFWEEK('2021-05-03');
-- invalid day
SELECT DAYOFWEEK('2021-2-30');

Free Resources