DAYOFWEEK
DAYOFWEEK in SQL is a function that returns the weekday index for a given date, where the index values range from 1 to 7. This function considers Sunday as the beginning of the week.
DAYOFWEEK(date)
- date: This parameter is the date from which the day of the week is to be returned. The date must be in a format that MySQL can recognize, such as ‘YYYY-MM-DD’ or ‘DD-MM-YYYY’. The function will return a number ranging from 1 (for Sunday) to 7 (for Saturday).
Example
Output
Explanation
The DAYOFWEEK function in MySQL returns the weekday index for a given date, where 1 represents Sunday and 7 represents Saturday. In this case, the function has returned the value 7, indicating that October 30, 2021, is a Saturday.
DAYOFWEEK(date)
- date: This parameter indicates the input date for which the day of the week is to be calculated. Acceptable formats for the date may include strings in standard SQL date format (‘YYYY-MM-DD’), as well as other typical date representations. The output of the function is an integer ranging from 1 to 7, representing the day of the week (where 1 stands for Sunday and 7 stands for Saturday) for the provided date.
Example
Output
Explanation
The DAYOFWEEK() function in SQL Server returns an integer value from 1 to 7 that represents a specific day of the week. In this example, the function returned 4, indicating it’s Wednesday. The weeks start from Sunday (1) and end on Saturday (7).