// dates & times reference

SQL SET DATEFIRST

A session setting declaring the first day of the week; it silently renumbers DATEPART(WEEKDAY) and re-anchors week truncation.

카테고리
Dates & times
Returns
session setting
In this category
102 of 121

구문

SET DATEFIRST n  -- 1 = Monday ... 7 = Sunday

엔진 지원

엔진Support메모
SQL Server 예 Supported under this name.
PostgreSQL Different name or syntax EXTRACT(ISODOW) is always Monday=1; DOW is Sunday=0
MySQL Different name or syntax DAYOFWEEK is ODBC Sunday=1; WEEKDAY is Monday=0
SQLite Different name or syntax strftime %w is always Sunday=0
Oracle Different name or syntax TO_CHAR(d, 'D') follows NLS_TERRITORY

예시

SQL Server

SET DATEFIRST 1; SELECT DATEPART(WEEKDAY, '2026-08-01');  -- 6 (Saturday, Monday=1)

PostgreSQL

SELECT EXTRACT(ISODOW FROM DATE '2026-08-01');  -- 6

MySQL

SELECT WEEKDAY('2026-08-01');  -- 5 (Monday=0)

알아 두면 좋은 정보

  • us_english defaults to 7 (Sunday first) while British and most European language settings use 1, so DATEPART(WEEKDAY, d) = 1 means Sunday on one login and Monday on another.
  • DATETRUNC(WEEK, d) also anchors on DATEFIRST: the same query truncates to Sunday or Monday depending on the connection.
  • Everywhere else the functions just differ by name instead of by session: PostgreSQL's ISODOW (Mon=1) vs DOW (Sun=0), MySQL's DAYOFWEEK (Sun=1) vs WEEKDAY (Mon=0).

Questions about SET DATEFIRST

How do I write week-start code that survives DATEFIRST?

Normalize with @@DATEFIRST: (DATEPART(WEEKDAY, d) + @@DATEFIRST - 2) % 7 + 1 is Monday = 1 under every setting. DATEADD(DAY, 1 - that value, CAST(d AS date)) is then the ISO Monday of d's week.

What is the portable weekday number?

There is none. Pick ISO (Monday = 1) and convert per engine: EXTRACT(ISODOW) on PostgreSQL, WEEKDAY(d) + 1 in MySQL, strftime('%u') on SQLite 3.44+, and the @@DATEFIRST formula above on SQL Server.

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.