DAYOFWEEK

Description

The DAYOFWEEK function is used to calculate and return the day of the week for a given date or time expression (expr). The returned integer value represents the day of the week, where 1 stands for Sunday, 2 stands for Monday, and so on, up to 7 which stands for Saturday.

Parameters

  • expr: The input date or time expression, which can be in string, timestamp, or other formats.

Return Value

Returns an integer representing the day of the week. 1 stands for Sunday, 2 stands for Monday, ..., 7 stands for Saturday.

Example

  1. Calculate the day of the week for a date in string format:
SELECT DAYOFWEEK('2022-03-31'); -- Result: 5

In the above example, the input date is March 31, 2022, and the return result is 5, indicating that this day is Thursday.

  1. Calculate the day of the week for a specific timestamp:
SELECT DAYOFWEEK(TIMESTAMP "2022-04-01 12:00:00"); -- Result: 6

In this example, the input timestamp represents 12:00 PM on April 1, 2022, and the returned result is 6, indicating that this day is a Friday.

Notes

  • Ensure that the input date or time expression is in the correct format, otherwise, it may lead to inaccurate calculation results.
  • This function is only applicable to date or time type data. For other types of data, it will return an error result.

Through the above example and explanation, you can better understand and use the DAYOFWEEK function to calculate the day of the week for a given date or time.