MySQL DAYOFWEEK() Function

MySQL: DAYOFWEEK() Function; In this tutorial, i am going to show you what is DAYOFWEEK() function and how to use it with the help of examples.

MySQL DAYOFWEEK() Function

The DAYOFWEEK() function returns the weekday index for a given date (a number from 1 to 7). Note: 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday.

Syntax of MySQL DAYOFWEEK() Function

The DAYOFWEEK() function syntax is:

DAYOFWEEK(date)

The date here is the date value that you want the day of the week from which you returned.

Example 1 – MySQL DAYOFWEEK() Function

Let’s take first example using mysql dayofweek() function; as follows:

SELECT DAYOFWEEK('2020-06-18') AS 'Result';

Output-1

+--------+
| Result |
+--------+
|     5  |
+--------+

Example 2 – MySQL DAYOFWEEK() Function

If there is a leading zero in the part of the day, then the leading zero has been left out of the result.

SELECT DAYOFWEEK('2019-03-15') AS 'Result';

Output-2

+--------+
| Result |
+--------+
|      6 |
+--------+

Example 3 – MySQL DAYOFWEEK() Function

Let’s take example with mysql query using dayofweek() function; as follows:

 SELECT
 created_at AS create_date,
 DAYNAME(created_at) AS 'Day Name', 
 DAYOFWEEK(created_at) AS day_of_week
 FROM users
 WHERE id= 112;

Output-3

+---------------------+-----------+-------------+
|  create_date        | Day Name  | Day of Week |
+---------------------+-----------+-------------+
| 2005-05-25 11:30:37 | Wednesday |           4 |
+---------------------+-----------+-------------+

Example 4 – MySQL DAYOFWEEK() with Now() Function

To extract part of the day from the current date and time (which is now returned using the () function); as follows:

  SELECT 
  NOW(),
  DAYOFWEEK(NOW());

Output-4

+---------------------+--------------------+
| NOW()               | DAYOFWEEK(NOW())   |
+---------------------+--------------------+
| 2019-07-10 18:30:44 |         6          |
+---------------------+--------------------+

Example 5 – MySQL DAYOFWEEK() Function

Let’s take example using curdate() and dayofweek(); as follows:

SELECT 
CURDATE(),
DAYOFWEEK(CURDATE());    

Output-5

+------------+-----------------------+
| CURDATE()  | DAYOFWEEK(CURDATE())  |
+------------+-----------------------+
| 2019-05-15 |              6        |
+------------+-----------------------+

Conclusion

MySQL dayofweek() function tutorial, You have learned how to use mysql DAYOFWEEK() function with various examples.

Leave a Comment