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. Using the CURDATE function to specify the date as an argument in the WEEKDAY function is a valid case. The function assigns 0 to Monday, 1 to Tuesday, 2 to Wednesday, … and 6 to Sunday. So all seven days a week have a digit attached to it which will be returned as the output. The function returns a valid index for a valid date but a ‘NULL’ value for an invalid date, a 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 input parameter for the function is the calendar date for which the index is to be retrieved.
We can see examples of the usage below:
How does the FORMAT function work 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 below.
SELECT WEEKDAY ('2020-01-01'), DAYNAME ('2020-01-01');
This query is to return the day index and day name of January 01, 2020.
So we can see that the output for the WEEKDAY function is two because it is the weekday index corresponding to January 01, 2020, which is a Wednesday.
We will write a query that returns the weekday index of a date when the argument is provided in the date_time format.
SELECT WEEKDAY('2020-01-10 20:00:00');
January 10, 2020, is a Friday; thus the output of the query is 4.
We saw valid scenarios in which we retrieved a good day index for the function. Let’s see an invalid date and corresponding output.
The month of February does not have day 30. So this is an invalid date. So the output is ‘NULL.’
The expected output is ‘NULL’ if no argument is passed in the function.
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 the WEEKDAY function with multiple date inputs within the same parenthesis will lead to an 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 a week in order.
SELECT WEEKDAY('2020-02-03') Feb_03,
We have used the WEEKDAY function seven times to get valid output.
When we examine the calendar, we can identify that February 03, 2020, falls on a Monday with an index value of 0. February 04 is a Tuesday with an index value of 1, February 05 is a Wednesday with an index value of 2, February 06 is a Thursday with an index value of 3, February 07 is a Friday with an index value of 4, February 08 is a Saturday with an index value of 5, and finally, February 09 is a Sunday with an index value of 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 the CURDATE function to retrieve the current date. Also, the CURDATE function is passed as an input argument in the DAYNAME function to retrieve the day name of the current date and in the WEEKDAY function to retrieve the day index of the current date.
If we study our output closely, we can understand that June 19, 2020, is returned as Today. The corresponding day name for June 19 is Friday; thus, the weekday function returns the output as 4.
Conclusion – MySQL WEEKDAY
The WEEKDAY function retrieves the index of days in a week and 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.
We hope that this EDUCBA information on “MySQL WEEKDAY” was beneficial to you. You can view EDUCBA’s recommended articles for more information.