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.
- 카테고리
- Dates & times
- Returns
date / timestamp- In this category
- 30 of 121
구문
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.
엔진 지원
| 엔진 | Support | 메모 |
|---|---|---|
| SQL Server | Different name or syntax | no typed DATE literal; ODBC escapes or unambiguous strings |
| PostgreSQL | 예 | Supported under this name. |
| MySQL | 예 | Supported under this name. |
| SQLite | Not built in | plain text strings serve as dates; no typed literal |
| Oracle | 예 | Supported under this name. |
예시
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';
알아 두면 좋은 정보
- 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.
Related on SQLly
- Add days to a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Add months to a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Truncate a date to day, month or year in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the first day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the last day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Parse text into a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, Oracle.
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.