Introduction to MySQL WEEKDAY
WEEKDAY function in MySQL returns the day index corresponding to a date. This function accepts only one argument at a time in either a date format or date_time format. It is a valid case to use the CURDATE function to specify the date as an argument in WEEKDAY function. The function assigns 0 to Monday, 1 to Tuesday, 2 to Wednesday, … and 6 to Sunday. So all the seven days of a week are having a digit attached to it which will be returned as the output. The function returns a valid index for a valid date, but returns ‘NULL’ value for an invalid date, 0 value, and blank parameters.
Let’s see the syntax and examples of this function.
The syntax for the WEEKDAY function is as below:
- The parameter input is the calendar date of which index is to be retrieved.
We can see examples of the usage as below:
How FORMAT function works in MySQL?
The WEEKDAY function returns an INT output, a digit index of the week corresponding to the calendar date. We can see different possible examples as below.
SELECT WEEKDAY ('2020-01-01'), DAYNAME ('2020-01-01');
This query is to return the day index and day name of 01 January 2020.
So we can see that, the output for WEEKDAY function is 2 because it is the week day index corresponding to 01 January, 2020, which is a Wednesday.
Let’s write a query to return the weekday index of a date when the argument is input in date_time format.
SELECT WEEKDAY('2020-01-10 20:00:00');
The 10th of January 2020, is a Friday and thus the output of the query is 4.
We saw valid scenarios in which we retrieve valid day index for the function. Let’s see an invalid date and corresponding output.
Month of February does not have day 30. So this is an invalid date. So the output is ‘NULL’.
If no argument is passed in the function, the expected output is ‘NULL’.
Just like an invalid date, a 0 date is also expected to return NULL as the output.
The date argument input ‘0000-00-00’ is an invalid date and thus it also returns NULL as output.
We already discussed that the WEEKDAY function picks only one argument in a line of query. If multiple dates are to be input, then a separate WEEKDAY function call must be made. Executing WEEKDAY function with multiple date inputs within same parenthesis will lead to error output.
We have received an error message as the query is an error. The response is “Error Code: 1582. Incorrect parameter count in the call to native function ‘WEEKDAY’”. And the response says the parameter count in the call is incorrect.
Let’s write a query to retrieve all seven days of a week, in order.
SELECT WEEKDAY('2020-02-03') Feb_03,
We have used the WEEKDAY function seven times to get valid output.
If we have a look at the calendar, we can identify that February 03, 2020, is a Monday and index value returned is 0, February 04 is Tuesday and index value returned is 1, February 05 is Wednesday and index value returned is 2, February 06 is Thursday and index value returned is 3, February 07 is Friday and index value returned is 4, February 08 is Saturday and index value returned is 5 and finally February 09 is a Sunday and index value returned is 6.
We can also try the CURDATE function within the WEEKDAY function to get the weekday index of the current date.
SELECT CURDATE() Today, DAYNAME (CURDATE()) Today_dayname, WEEKDAY(CURDATE()) Today_weekday;
Query calls CURDATE function to retrieve the current date, also the CURDATE function is passed as an input argument in DAYNAME function to retrieve the day name of the current date, and in WEEKDAY function to retrieve day index of the current date.
If we study our output closely, we can understand, the current date, 19th June 2020 is returned as Today. Corresponding day name for 19th June is Friday and thus the weekday function returns the output as 4.
Conclusion – MySQL WEEKDAY
WEEKDAY function is used to retrieve the index of days in a week and it always outputs an INT value. The valid input formats are CURDATE() function, DATE format, and DATE_TIME format. The function allows only one argument per function call. Invalid date, zero date, and blank values return ‘NULL’ as output in this function. The function assigns an index of 0 to Monday, 1 to Tuesday, and thus 6 to Sunday.
This is a guide to MySQL WEEKDAY. Here we discuss an introduction to MySQL WEEKDAY, syntax how does it work with query examples. You can also go through our other related articles to learn more –