SQL Tutorial SQL Advanced SQL Database SQL References

MySQL DAYOFWEEK() Function



The MySQL DAYOFWEEK() function returns the weekday index of a given date or datetime value where 1=Sunday, 2=Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday.

Syntax

DAYOFWEEK(datetime)

Parameters

datetime Required. Specify a date or datetime value from which to extract the weekday index.

Return Value

Returns the weekday index of a given date or datetime value.

Example 1:

The example below shows the usage of DAYOFWEEK() function.

mysql> SELECT DAYOFWEEK('2018-08-18');
Result: 7

mysql> SELECT DAYOFWEEK('2018-08-18 10:38:42');
Result: 7

mysql> SELECT DAYOFWEEK('2018-08-18 10:38:42.000004');
Result: 7

mysql> SELECT DAYOFWEEK('2014-10-20');
Result: 2

mysql> SELECT DAYOFWEEK(CURDATE());
Result: 4

Example 2:

Consider a database table called Orders with the following records:

OrderQuantityPriceOrderTime
1001.582017-08-18 10:38:42
1201.612018-03-23 07:14:16
1251.782018-09-12 05:25:56
501.802019-01-16 11:52:05
2001.722020-02-06 09:31:34

The statement given below can be used to get the weekday index of records of column OrderTime:

SELECT *, DAYOFWEEK(OrderTime) AS DAYOFWEEK_Value FROM Orders;

This will produce the result as shown below:

OrderQuantityPriceOrderTimeDAYOFWEEK_Value
1001.582017-08-18 10:38:426
1201.612018-03-23 07:14:166
1251.782018-09-12 05:25:564
501.802019-01-16 11:52:054
2001.722020-02-06 09:31:345

❮ MySQL Functions