w3resource

MySQL WEEKDAY() function

WEEKDAY() function

MySQL WEEKDAY() returns the index of the day in a week for a given date (0 for Monday, 1 for Tuesday and ......6 for Sunday).

This function is useful in -

  • WEEKDAY() is used to allocate resources efficiently based on the nature of the tasks and the availability of resources on specific weekdays.
  • It's used to trigger alerts or notifications based on specific weekdays, allowing for the automation of certain tasks or reminders.
  • WEEKDAY() is helpful for segmenting or categorizing data by weekdays, which can be valuable in various reporting and analysis scenarios.
  • It's used in event planning to schedule events on days that are most convenient or likely to attract a larger audience.
  • WEEKDAY() can be used in predictive analysis to forecast trends or behaviors that are influenced by specific weekdays.
  • For businesses, it can be used to identify which days of the week experience the highest or lowest levels of activity.
  • In logistics and delivery systems, WEEKDAY() helps in planning routes and deliveries based on factors like traffic patterns on specific weekdays.

Syntax:

WEEKDAY(date)

Where date is a date.

Syntax Diagram:

MySQL WEEKDAY() Function - Syntax Diagram

MySQL Version: 8.0


Pictorial Presentation:

Pictorial Presentation of MySQL WEEKDAY() function

Example: MySQL WEEKDAY() function

The following statement will return the index of the week for the date 2009-05-19.

Code:

SELECT WEEKDAY('2009-05-19');

Output:

mysql> SELECT WEEKDAY('2009-05-19');
+-----------------------+
| WEEKDAY('2009-05-19') |
+-----------------------+
|                     1 | 
+-----------------------+
1 row in set (0.00 sec)

Example: WEEKDAY() function using on table

The following statement will return the invoice_no, receive_dt and index of the week for receive_dt, after making sure that index of the week for receive_dt must be more than 0 and less than 6 (i.e. it should be from Monday through Saturday).

Sample table: purchase


Code:

 SELECT invoice_no,receive_dt,WEEKDAY(receive_dt)
      FROM purchase           
      WHERE WEEKDAY(receive_dt)>0 
      AND WEEKDAY(receive_dt)<6;

Output:

mysql> SELECT invoice_no,receive_dt,WEEKDAY(receive_dt)
    ->       FROM purchase           
    ->       WHERE WEEKDAY(receive_dt)>0 
    ->       AND WEEKDAY(receive_dt)< 6;
+------------+------------+---------------------+
| invoice_no | receive_dt | WEEKDAY(receive_dt) |
+------------+------------+---------------------+
| INV0001    | 2008-07-19 |                   5 | 
| INV0002    | 2008-08-28 |                   3 | 
| INV0003    | 2008-09-23 |                   1 | 
| INV0004    | 2007-08-30 |                   3 | 
+------------+------------+---------------------+
4 rows in set (0.06 sec)

Video Presentation:

All Date and Time Functions:

Click here to see the MySQL Date and time functions.

Previous: WEEK()
Next: WEEK OF YEAR()



Follow us on Facebook and Twitter for latest update.