MySQL WEEKDAY() Function

MySQL WEEKDAY() function; Through this tutorial, i am going to show you MySQL WEEKDAY() function with the help of examples.

MySQL WEEKDAY() Function

WEEKDAY() function in MySQL is used to find the weekday value for a given date. If the date is NULL, the WEEKDAY() function will return NULL. Otherwise, it returns index for a date i.e., 0 for Monday, 1 for Tuesday, … 6 for Sunday.

Syntax of MySQL WEEKDAY() Function

The WEEKDAY() function syntax is:

WEEKDAY(date)

Here date is the date value that you want return the weekday name from.

Example 1 – MySQL WEEKDAY() Function

Let’s take an example using mysql weekday() funtion; as follows:

SELECT WEEKDAY('2019-07-11') AS 'Result';

Output-1

 +---------+
 | Result  |
 +---------+
 |    3    |
 +---------+

Example 2 – MySQL WEEKDAY() Function

To get the week day of a table in the database using WEEKDAY() with mysql queries; as follows:

 SELECT
 created_at AS create_date,
 WEEKDAY(created_at) AS week_day
 FROM users
 WHERE id = 1;

Output-2

 +---------------------+--------------+
 | create_date         | week_day     |
 +---------------------+--------------+
 | 2019-07-11 11:30:37 |        3     |
 +---------------------+--------------+

Example 3 – MySQL WEEKDAY() VS dayofweeek()

Let’s take an example to differentiate weekday() and dayofweek() function; as follows:

 SET @date = '2019-07-13';
 SELECT 
   DAYOFWEEK(@date) AS 'Day OF WEEK',
   WEEKDAY(@date) AS 'Weekday';
WEEKDAY()DayofWeek()
MySQL WEEKDAY() function returns the weekday number for a given date(between 0 to 6).MySQL DAYOFWEEK() function returns the weekday index for a given date (between 1 to 7).
1 to 7 means:
1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday.
0 to 6 means:
0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.

Output-3

+----------+----------------+
| Day Of Name | Weekday     |
+-------------+-------------+
| 7           |       5     |
+-------------+-------------+

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

Take an example using current date with WEEKDAY() function; as follows:

 SELECT 
 NOW(),
 WEEKDAY(NOW());

Output-4

 +---------------------+----------------+
 | NOW()               | WEEKDAY(NOW()) |
 +---------------------+----------------+
 | 2018-07-13 10:05:41 |   5            |
 +---------------------+----------------+

Example 5 – MySQL WEEKDAY() Function

Let’s take example using curdate() with WEEKDAY() function; as follows:

 SELECT 
CURDATE(),
WEEKDAY(CURDATE());

Output-5

 +------------+--------------------+
 | CURDATE()  | WEEKDAY(CURDATE()) |
 +------------+--------------------+
 | 2019-07-13 |     5              |
 +------------+--------------------+

Conclusion

MySQL weekday() function tutorial, You have learned how to use WEEKDAY() function with various examples.

Leave a Comment