// dates & times reference

SQL day of week function

Returns which weekday a date falls on. No two engines agree on the function or the numbering: 0 or 1 for Sunday, 1 for Monday, or whatever a session setting says.

Категория
Dates & times
Returns
integer 0-6 or 1-7, engine-dependent
In this category
7 of 121

Синтаксис

EXTRACT(dow FROM d)  -- PostgreSQL
DAYOFWEEK(d)  -- MySQL
DATEPART(weekday, d)

Each block is a separate form. The support table and the examples show which engine takes which.

Поддержка движка

ДвижокSupportЗаметки
SQL Server Different name or syntax DATEPART(weekday, d), first day set by DATEFIRST
PostgreSQL Different name or syntax EXTRACT(dow) is 0=Sunday; EXTRACT(isodow) is 1=Monday
MySQL Different name or syntax DAYOFWEEK is 1=Sunday; WEEKDAY is 0=Monday
SQLite Different name or syntax strftime('%w', d), 0=Sunday, as text; '%u' (1=Monday) from 3.44
Oracle Different name or syntax TO_CHAR(d, 'D') follows NLS_TERRITORY; TO_CHAR(d, 'DY') gives the name

Примеры

PostgreSQL

SELECT EXTRACT(isodow FROM created_at) AS weekday FROM orders;

MySQL

SELECT WEEKDAY(created_at) AS weekday FROM orders;  -- 0 = Monday

SQL Server

SELECT DATEPART(WEEKDAY, created_at) AS weekday FROM orders;

SQLite

SELECT strftime('%w', created_at) AS weekday FROM orders;  -- '0' = Sunday

Oracle

SELECT TO_CHAR(created_at, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') AS weekday
FROM orders;  -- MON, TUE, ...

Полезно знать

  • PostgreSQL gives you both conventions: dow (0 = Sunday) and isodow (1 = Monday). Pick isodow for business-week logic.
  • MySQL's DAYOFWEEK (1 = Sunday) and WEEKDAY (0 = Monday) number the same day differently: Monday is 2 in one and 0 in the other.
  • Oracle's TO_CHAR(d, 'D') follows the session's NLS_TERRITORY, so the same query returns different numbers in different countries. Comparing names with an explicit language is stable: TO_CHAR(d, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH').
  • SQL Server's DATEPART(weekday) depends on SET DATEFIRST, which changes with the login's language. (DATEPART(weekday, d) + @@DATEFIRST - 2) % 7 + 1 gives 1 = Monday under any setting.

Questions about Day of week

Why do engines number weekdays differently?

The US convention starts the week on Sunday and ISO 8601 starts it on Monday, and engines picked different ones. PostgreSQL has dow (0=Sunday) and isodow (1=Monday); MySQL has two functions with different bases; SQL Server follows DATEFIRST; Oracle follows NLS_TERRITORY. Check the convention before filtering.

How do I filter for weekends only?

PostgreSQL: EXTRACT(isodow FROM d) IN (6, 7). MySQL: WEEKDAY(d) >= 5. SQL Server: DATEPART(WEEKDAY, d) IN (1, 7) with the us_english DATEFIRST of 7. SQLite: strftime('%w', d) IN ('0', '6'). Oracle: TO_CHAR(d, 'DY', 'NLS_DATE_LANGUAGE=ENGLISH') IN ('SAT', 'SUN').

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.