SQL DATE_TRUNC function
Rounds a timestamp down to the start of a day, week, month or year. It is how time series get grouped into one bucket per period.
- Categoría
- Dates & times
- Returns
timestamp / date- In this category
- 5 of 121
Sintaxis
DATE_TRUNC('month', ts) -- PostgreSQL
DATETRUNC(month, ts) -- SQL Server 2022
Each block is a separate form. The support table and the examples show which engine takes which.
Soporte del motor
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Different name or syntax | DATETRUNC from SQL Server 2022; DATEFROMPARTS before that |
| PostgreSQL | Sí | Supported under this name. |
| MySQL | Different name or syntax | no DATE_TRUNC; DATE_FORMAT(d, '%Y-%m-01') or arithmetic |
| SQLite | Different name or syntax | date(ts, 'start of month') or strftime('%Y-%m-01', ts) |
| Oracle | Different name or syntax | TRUNC(date, 'MM'); 'IW' for ISO weeks, 'YYYY' for years |
Ejemplos
PostgreSQL
SELECT DATE_TRUNC('month', created_at) AS month,
COUNT(*) AS orders
FROM orders
GROUP BY 1 ORDER BY 1;
SQL Server
SELECT DATETRUNC(month, created_at) AS month, COUNT(*) AS orders FROM orders GROUP BY DATETRUNC(month, created_at);
MySQL
SELECT DATE_FORMAT(created_at, '%Y-%m-01') AS month, COUNT(*) AS orders FROM orders GROUP BY month;
Conviene saberlo
- Week starts differ: PostgreSQL's DATE_TRUNC('week', ts) starts on Monday (ISO). SQL Server's DATETRUNC(week, d) follows SET DATEFIRST, Sunday under us_english; DATETRUNC(iso_week, d) starts Monday.
- The pre-2022 SQL Server idiom rebuilds the date: DATEFROMPARTS(YEAR(d), MONTH(d), 1).
- MySQL and SQLite get there by formatting: DATE_FORMAT(d, '%Y-%m-01') on MySQL returns a string, and SQLite's date(ts, 'start of month') returns text like every SQLite date.
- On PostgreSQL, DATE_TRUNC on a date returns a timestamp (with time zone); cast back with ::date when you want a date.
Questions about DATE_TRUNC
Does SQL Server have DATE_TRUNC?
SQL Server 2022 added DATETRUNC(month, d). Before that, rebuild the date: DATEFROMPARTS(YEAR(d), MONTH(d), 1) for months, or DATEADD(month, DATEDIFF(month, 0, d), 0), which also works for other units.
Why does DATE_TRUNC('week') start on Monday?
PostgreSQL uses ISO weeks, which start on Monday. SQL Server's DATETRUNC(week) follows DATEFIRST, so the same query can bucket differently per session language. Use DATETRUNC(iso_week, d) or SET DATEFIRST 1 to match PostgreSQL.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- 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
- Get the first and last day of the month in SQL Snippet
- Get the first and last day of the quarter in SQL Snippet
- Get the first and last day of the week in SQL Snippet
Cheat sheets for the engines that have it: PostgreSQL, SQL Server, MySQL, SQLite, 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.