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.
Function Syntax
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:
- Sunday
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
Example
The following code shows the example usage of the DAYOFWEEK() function:
DAYOFWEEK('2022-10-10') as day_index;
The query should return the index from the provided date, as shown below:
---------+
2|
We can pair the DAYOFWEEK() function with DAYNAME(), as shown below:
DAYNAME('2022-10-10')as day,
DAYOFWEEK('2022-10-10') as day_index;
Output:
Conclusion
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.
Source: linuxhint.com