// dates & times reference

SQL DateStyle parameter

A session GUC controlling timestamp output style and, for non-ISO input, the month/day order when parsing ambiguous date literals.

类别
Dates & times
Returns
session GUC
In this category
103 of 121

语法

SET datestyle = 'ISO, MDY'

引擎支持

引擎Support备注
SQL Server Different name or syntax SET DATEFORMAT / SET LANGUAGE for the parsing side
PostgreSQL 是 this GUC: 'ISO, DMY' and friends
MySQL Not built in No built-in equivalent.
SQLite Not built in No built-in equivalent.
Oracle Different name or syntax NLS_DATE_FORMAT + NLS_DATE_LANGUAGE

示例

PostgreSQL

SET datestyle = 'ISO, DMY';  SELECT '04/05/2026'::date;  -- 2026-05-04

PostgreSQL

SHOW datestyle;  -- check before relying on implicit parsing

值得了解

  • With ISO output the second component (MDY/DMY) is ignored for display; ISO is always YYYY-MM-DD. It still governs how '04/05/2026' is read: MDY says April 5, DMY says May 4.
  • The other styles (German, SQL, Postgres) change output to locale-flavoured text; code that concatenates or compares now()::text output breaks when the GUC differs between sessions.
  • Never parse ambiguous slash dates; use DATE '2026-08-01' typed literals or unambiguous YYYY-MM-DD strings, and leave DateStyle at ISO.

Questions about DateStyle

Does DateStyle affect to_char?

No: to_char always applies its own mask. DateStyle shapes implicit casts and default output only, which is exactly where the surprises live.

How is this different from SQL Server's SET DATEFORMAT?

Same parsing role (day/month order for ambiguous literals); DateStyle additionally switches the output format family, which DATEFORMAT never does.

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