SQL TRUNC date function
Returns the date floored to the boundary named by an optional format mask; with no mask it strips the time of day.
- Returns
date- In this category
- 86 of 121
語法
TRUNC(date)
TRUNC(date, fmt)
Each block is a separate form. The support table and the examples show which engine takes which.
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | DATETRUNC on 2022+, DATEADD idiom before |
| PostgreSQL | Different name or syntax | date_trunc('day', d) |
| MySQL | Different name or syntax | DATE(d) for the day form; DATE_FORMAT tricks for the rest |
| SQLite | Different name or syntax | date(d) strips time; modifiers truncate further |
| Oracle | 是 | Supported under this name. |
範例
Oracle
SELECT TRUNC(SYSDATE) FROM dual; -- today at midnight
Oracle
SELECT TRUNC(DATE '2026-08-15', 'IW') FROM dual; -- 2026-08-10 (Monday)
Oracle
SELECT TRUNC(order_date, 'MM') AS month_start, COUNT(*) FROM orders GROUP BY TRUNC(order_date, 'MM');
小知識
- TRUNC(d) alone zeroes the time, the every-day idiom for joining on dates or stripping times from SYSDATE.
- The mask names the unit: 'IW' gives the ISO Monday, 'MM' the month's first day, 'Q' the quarter start, 'YYYY' the year start, and 'DAY' the first day of the week under the session's NLS_TERRITORY.
- TRUNC always returns a DATE: on a TIMESTAMP input it drops fractional seconds and any zone. CAST the result back to TIMESTAMP if the column type matters.
Questions about TRUNC (date)
What is the PostgreSQL equivalent of TRUNC(d, 'IW')?
date_trunc('week', d); both give the ISO Monday. For Sunday-start weeks, Oracle's TRUNC(d, 'DAY') follows NLS_TERRITORY; PostgreSQL has no such setting, so shift by hand: date_trunc('week', d + INTERVAL '1 day') - INTERVAL '1 day'.
Does TRUNC round or floor?
Floor, always: anything after the boundary is discarded. ROUND(date) is the nearest-boundary cousin.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, 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.