Returns the day of the week for a DATETIME value as a BIGINT (0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday).
Syntax
bigint weekday(datetime <date>)Parameters
date: Required. A DATETIME value in yyyy-mm-dd hh:mi:ss format. If the MaxCompute V1.0 data type edition is enabled in your project, a STRING value in the same format is implicitly converted to DATETIME before the calculation.
Return value
Returns a BIGINT from 0 (Monday) to 6 (Sunday). Returns null if date is null. Returns an error if date is neither DATETIME nor STRING, or if the format does not match yyyy-mm-dd hh:mi:ss.
Examples
Static values
-- 2009-03-20 is a Friday. Returns 4.
select weekday(datetime '2009-03-20 11:11:00');
-- STRING input with MaxCompute V1.0 data type edition. Returns 4.
set odps.sql.type.system.odps2=false;
select weekday('2009-03-20 11:11:00');
-- Returns null.
select weekday(null);Table data
The following example uses the mf_date_fun_t sample table. To create and populate this table, see Date functions.
select datetime1, weekday(datetime1) as datetime1_weekday from mf_date_fun_t;Result:
+---------------------+-------------------+
| datetime1 | datetime1_weekday |
+---------------------+-------------------+
| 2021-11-29 00:01:00 | 0 |
| 2021-11-28 00:02:00 | 6 |
| 2021-11-27 00:03:00 | 5 |
| 2021-11-26 00:04:00 | 4 |
| 2021-11-25 00:05:00 | 3 |
| 2021-11-24 00:06:00 | 2 |
| 2021-11-23 00:07:00 | 1 |
| 2021-11-22 00:08:00 | 0 |
| 2021-11-21 00:09:00 | 6 |
| 2021-11-20 00:10:00 | 5 |
+---------------------+-------------------+Related functions
WEEKDAY is a date function. For more information, see Date functions.