MySQL DAYOFWEEK function is one of the Date Function, used to return the weekday index value of the given date. This function returns value from 1 to 7 where 1 = Sunday, 2 = Monday, ….. 7 = Sunday.
In this article, we will show you, How to use this DAYOFWEEK function to get the weekday or Day of Week in MySQL from an expression with example.
MySQL DAYOFWEEK Syntax
The basic syntax of the DAYOFWEEK() Function in MySQL is as shown below:
DAYOFWEEK(date or expression);
MySQL DAYOFWEEK function Example
These examples helps you understand the use of dayofweek function. Here, we are returning the Weekday index number from the date expression, and the Date & time expression.
SELECT DAYOFWEEK('2018-12-15 03:22:19');
SELECT DAYOFWEEK(NOW()), DAYOFWEEK(CURRENT_DATE());
MySQL DAYOFWEEK Function Example 2
The following queries will show you, what happens when we try to return the weekday index from 0 date format, string format, or invalid dates.
SELECT DAYOFWEEK(NOW() + 2);
From the above screenshot, DAYOFWEEK(‘2019-02-31’); statement returned NULL. Because, it’s an invalid date.
Within the Last statement, DAYOFWEEK(0) means, we are trying to extract day of week from a zero date part. That’s why, it returns NULL.
Thank You for Visiting Our Blog