MySQL WEEKDAY() Function Examples

MySQL WEEKDAY() Function Examples

MySQL WEEKDAY() function; Through this tutorial, we will learn MySQL WEEKDAY() function. We will provide many examples of MySQL WEEKDAY() with its syntax.

And as well as, 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.

Recommended MySQL Tutorials

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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