// dates & times reference

SQL NLS_DATE_FORMAT parameter

Returns and sets the session's implicit date mask; TO_DATE with an explicit mask is the only way to not depend on it.

類別
Dates & times
Returns
text
In this category
64 of 121

語法

SELECT VALUE FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_DATE_FORMAT'
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD'

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

引擎支援

引擎Support備註
SQL Server Different name or syntax DATEFORMAT and LANGUAGE settings play this role
PostgreSQL Different name or syntax DateStyle sets the output format and the DMY/MDY order for ambiguous input
MySQL Not built in fixed ISO-ish expectations for implicit casts
SQLite Not built in no implicit parsing beyond ISO-8601 text
Oracle 是 Supported under this name.

範例

Oracle

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';

Oracle

SELECT VALUE FROM NLS_SESSION_PARAMETERS
WHERE PARAMETER = 'NLS_DATE_FORMAT';

Oracle

SELECT COUNT(*) FROM orders
WHERE created_at >= TO_DATE('2024-01-01', 'YYYY-MM-DD');

小知識

  • Comparing a DATE column to a bare string parses the string with NLS_DATE_FORMAT: under the American default DD-MON-RR, '01-JAN-24' works and '2024-01-01' raises ORA-01861.
  • The default comes from NLS_TERRITORY, and clients can override it through NLS_LANG or their own settings, so the same query works in SQL Developer and fails in a scheduler job. Explicit TO_DATE masks end that class of bug.
  • NLS_DATE_FORMAT covers whole seconds but not fractional or zone-aware values; NLS_TIMESTAMP_FORMAT and NLS_TIMESTAMP_TZ_FORMAT are the siblings.

Questions about NLS_DATE_FORMAT

Why does my date query work locally but fail in production?

The sessions have different NLS_DATE_FORMAT values. Implicit string-to-date conversion depends on it; use TO_DATE with an explicit mask everywhere.

Does NLS_DATE_FORMAT affect TO_DATE?

Only when you leave the mask out: TO_DATE('2024-01-01') uses the session mask. TO_DATE('2024-01-01', 'YYYY-MM-DD') never does.

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

// 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.