MySQL WEEKDAY() Function
The MySQL WEEKDAY() function returns the weekday index of a given date or datetime value where 0=Monday, 1=Tuesday, 2=Wednesday, 3=Thursday, 4=Friday, 5=Saturday, 6=Sunday.
Syntax
WEEKDAY(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 WEEKDAY() function.
mysql> SELECT WEEKDAY('2018-08-18'); Result: 5 mysql> SELECT WEEKDAY('2018-08-18 10:38:42'); Result: 5 mysql> SELECT WEEKDAY('2018-08-18 10:38:42.000004'); Result: 5 mysql> SELECT WEEKDAY('2014-10-20'); Result: 0 mysql> SELECT WEEKDAY(CURDATE()); Result: 2
Example 2:
Consider a database table called Orders with the following records:
OrderQuantity | Price | OrderTime |
---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 |
120 | 1.61 | 2018-03-23 07:14:16 |
125 | 1.78 | 2018-09-12 05:25:56 |
50 | 1.80 | 2019-01-16 11:52:05 |
200 | 1.72 | 2020-02-06 09:31:34 |
The statement given below can be used to get the weekday index of records of column OrderTime:
SELECT *, WEEKDAY(OrderTime) AS WEEKDAY_Value FROM Orders;
This will produce the result as shown below:
OrderQuantity | Price | OrderTime | WEEKDAY_Value |
---|---|---|---|
100 | 1.58 | 2017-08-18 10:38:42 | 4 |
120 | 1.61 | 2018-03-23 07:14:16 | 4 |
125 | 1.78 | 2018-09-12 05:25:56 | 2 |
50 | 1.80 | 2019-01-16 11:52:05 | 2 |
200 | 1.72 | 2020-02-06 09:31:34 | 3 |
❮ MySQL Functions