MySQL DAYOFWEEK Function
This short post will discuss using the DAYOFWEEK() function in MySQL. This is a simple but valuable date and time function in MySQL.
The DAYOFWEEK() function lets you get the weekday index from a given date input. The function returns 1 for Sunday, 2 for Tuesday, etc. This corresponds to the ODBC standard.
The function returns a NULL value if the input date value is NULL.
The following code snippet shows the function syntax and accepted parameters:
The function accepts the target date as the parameter. The input value must be a DATE or DATETIME type.
The function will then return an integer type, each representing the day of the week as shown below:
The following code shows the example usage of the DAYOFWEEK() function:
The query should return the index from the provided date, as shown below:
We can pair the DAYOFWEEK() function with DAYNAME(), as shown below:
In this simple tutorial, we discussed how to use the DAYOFWEEK() function in MySQL to return a day’s index from a given date. In addition, an example is provided with the DAYNAME() function.