// dates & times reference

SQL date literal

Returns a typed date value straight from SQL text. DATE '...' and TIMESTAMP '...' avoid implicit string-to-date conversions and their format guessing.

Categoria
Dates & times
Returns
date / timestamp
In this category
30 of 121

Sintaxe

DATE '2024-01-15'
TIMESTAMP '2024-01-15 09:30:00'
{d '2024-01-15'}  -- ODBC escape, SQL Server

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

Suporte a mecanismos

MotorSupportNotas
SQL Server Different name or syntax no typed DATE literal; ODBC escapes or unambiguous strings
PostgreSQL Sim Supported under this name.
MySQL Sim Supported under this name.
SQLite Not built in plain text strings serve as dates; no typed literal
Oracle Sim Supported under this name.

Exemplos

PostgreSQL

SELECT * FROM orders
WHERE created_at >= DATE '2024-01-01';

Oracle

SELECT * FROM orders
WHERE created_at >= TIMESTAMP '2024-01-01 00:00:00';

SQL Server

SELECT * FROM orders
WHERE created_at >= '20240101';

Bom saber

  • SQL Server rejects DATE '2024-01-15'. For the legacy datetime type even '2024-01-02' reads as February 1 under SET DATEFORMAT dmy; 'YYYYMMDD' and 'YYYY-MM-DDThh:mm:ss' parse the same under every setting.
  • Oracle's DATE literal still carries the midnight time; TIMESTAMP '...' is the literal that keeps sub-second fields.
  • MySQL accepts both the standard typed literal and ODBC escapes like {d '2024-01-15'}. SQLite has no typed literal at all: DATE '2024-01-15' fails there with no such column: DATE, and dates are plain text.

Questions about DATE / TIMESTAMP literals

Why does SQL Server prefer 20240101 over dashes?

Language and DATEFORMAT settings make 01-02-2024 ambiguous: it flips between January 2 and February 1. YYYYMMDD parses the same under every setting, for every date type.

Are date literals portable?

DATE 'YYYY-MM-DD' works on PostgreSQL, MySQL and Oracle; SQL Server and SQLite need string or ODBC-escape stand-ins.

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.