MySQL WEEKDAY() Function

The WEEKDAY() function in MySQL is used to return the weekday number for a given date. It assigns a numerical value to each day of the week, with Monday being 0 and Sunday being 6.


Definition and Usage

  • Purpose: The function returns an integer value representing the day of the week for a given date.
    • Monday is represented by 0
    • Tuesday is represented by 1
    • Wednesday is represented by 2
    • Thursday is represented by 3
    • Friday is represented by 4
    • Saturday is represented by 5
    • Sunday is represented by 6

Syntax

WEEKDAY(date)
  • Parameters:
    • date: Required. The date or datetime value for which you want to extract the weekday number.

Technical Details

  • Works in: From MySQL 4.0 onwards.
  • Return Type: Returns an integer value representing the weekday number.

Examples

Example 1: Return the weekday number for a specific date

SELECT WEEKDAY("2017-06-15");

Result:

3

Explanation: June 15, 2017, was a Thursday, which corresponds to the weekday number 3.


Example 2: Return the weekday number for January 1st, 2017

SELECT WEEKDAY("2017-01-01");

Result:

6

Explanation: January 1, 2017, was a Sunday, which corresponds to the weekday number 6.


Example 3: Return the weekday number for the current system date

SELECT WEEKDAY(CURDATE());

Result:

<weekday number of today's date>

Explanation: This will return the weekday number for the current system date.


Use Cases

  • Scheduling: Helps in scheduling tasks or events based on the day of the week.
  • Data Filtering: Can be used to filter or aggregate data based on weekdays.
  • Custom Weekdays: Useful for custom applications that may require a weekday number in a specific format or numbering system.

Related Functions

  • DAYOFWEEK(): Returns the weekday number, but with Sunday as 1 and Saturday as 7.
  • DAYNAME(): Returns the full name of the weekday for a given date.

Was this article helpful?