| by Arround The Web | No comments

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:

DAYOFWEEK(date)

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:

  1. Sunday
  2. Monday
  3. Tuesday
  4. Wednesday
  5. Thursday
  6. Friday
  7. Saturday

Example

The following code shows the example usage of the DAYOFWEEK() function:

select
    DAYOFWEEK('2022-10-10') as day_index;

The query should return the index from the provided date, as shown below:

day_index|
---------+
    2|

We can pair the DAYOFWEEK() function with DAYNAME(), as shown below:

select
    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.

Share Button

Source: linuxhint.com

Leave a Reply