SQL DATE() function
Returns the date component with the time removed, the standard form for grouping timestamps by day and for date-only joins.
- Catégorie
- Dates & times
- Returns
date- In this category
- 49 of 121
Syntaxe
DATE(ts) -- MySQL, SQLite
CAST(ts AS date) -- SQL Server
ts::date -- PostgreSQL
TRUNC(d) -- Oracle
Each block is a separate form. The support table and the examples show which engine takes which.
Prise en charge des moteurs
| Moteur | Support | Notes |
|---|---|---|
| SQL Server | Different name or syntax | CAST(ts AS date); no DATE() function |
| PostgreSQL | Oui | date(ts) works as function-style cast syntax; ts::date is the usual spelling |
| MySQL | Oui | Supported under this name. |
| SQLite | Oui | Supported under this name. |
| Oracle | Different name or syntax | TRUNC(d); Oracle's DATE type already holds a time |
Exemples
MySQL
SELECT DATE(created_at) AS day, COUNT(*) FROM orders GROUP BY day;
PostgreSQL
SELECT created_at::date AS day, COUNT(*) FROM orders GROUP BY 1;
SQL Server
SELECT CAST(created_at AS date) AS day, COUNT(*) FROM orders GROUP BY CAST(created_at AS date);
Oracle
SELECT TRUNC(created_at) AS day, COUNT(*) FROM orders GROUP BY TRUNC(created_at);
Bon à savoir
- Oracle's DATE type has a time inside: TRUNC(d) is not cosmetic, it is what makes equality comparisons hit midnight matches.
- On a timestamptz, PostgreSQL's ::date uses the session time zone, so the same instant can fall on different dates in different sessions.
- Filtering on DATE(created_at) = '2024-01-15' blocks a plain index on most engines; created_at >= '2024-01-15' AND created_at < '2024-01-16' keeps it. SQL Server is an exception: CAST(col AS date) stays seekable.
Questions about DATE()
Is DATE() the same as CAST AS date?
Same result, engine spelling differs. MySQL and SQLite call it DATE(); SQL Server casts; PostgreSQL casts (date_trunc('day', ts) keeps the timestamp type instead). SQLite's DATE() returns 'YYYY-MM-DD' text.
Why is my date-equality join missing rows?
The column still has a time: 2024-01-15 09:30 is not equal to 2024-01-15. Compare DATE(ts) = DATE(d2) or use half-open ranges.
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: SQLite, 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.