WEEKDAY() Function Examples – MySQL

In this MySQL tutorial, you will learn how MySQL WEEKDAY() function. We will provide many examples of mysql WEEKDAY() with it’s syntax.

We will also explain the difference between Mysql WEEKDAY() vs DAYOFWEEK() with example.

MySQL WEEKDAY() Function

In MySQL, WEEKDAY function is used to returns the weekday index for a date. Just provide the date as a argument(params) and the it will return the weekday index.

Syntax

The WEEKDAY() function syntax is:

WEEKDAY(date)

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

Examples

Now we take different different type of examples of WEEKDAY function. See below all the examples one by one.

Example-1

First simple example to demonstrate.

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

Output-1

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

Example-2 With Database Queries

For some time we want to fetch a record / data from the mysql database table. When we need to get the week day of a table in the database. In that case we use WEEKDAY() with mysql queries.

Next, we take example-2, in this example we will fetch WEEKDAY of database table name users where column name is created_at.

 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 of WEEKDAY() vs DAYOFWEEK()

In example-3, we take an example to differentiate weekday() and dayofweek() function.

 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 Using Current Date/Time

Now we take new example using current date with WEEKDAY() function. It will return weekday from the current date & time.

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

Output-4

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

Example-5 Using MySQL CURDATE() Function

Next, we will take another example of WEEKDAY() with CURDATE() function. Which is returns only the weekday.

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

Output-5

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

Example-6 with DAYNAME() Function

We will take a comparison example to simplify our example. Here we compare the WEEKDAY() and DAYNAME() functions which make our example easy.

Here, WEEKDAY function is used to returns the weekday index for a date. And DAYNAME gives the name of the date given as the string format.

 SET @date = '2020-01-25';
 SELECT 
   DAYNAME(@date) AS 'Day Name',
   WEEKDAY(@date) AS 'Weekday';

Output-6

+----------+---------+
| Day Name | Weekday |
+----------+---------+
| Saturday |       5 |
+----------+---------+

Conclusion

Here, You have learned how to use WEEKDAY() function with various examples.

If you have any questions or thoughts to share, use the comment form below to reach us.

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *