DATE_TRUNC(precision, source) truncates the datetime value source to the start of the time unit specified by precision.
Note
This function is available starting from V4.6.0. It is available only in MySQL-compatible mode.
Syntax
DATE_TRUNC(precision, source)
Parameters
Parameter |
Description |
|---|---|
precision |
The time unit to which the value is truncated. The value is case-insensitive. For more information, see the table below. |
source |
The datetime value to be truncated. DATETIME and DATE values (including those in MySQL-compatible mode) are returned in their original types; other types are treated as DATETIME and returned as DATETIME. |
Supported values of precision
Value |
Truncated to |
|---|---|
microseconds |
The start of the microsecond. |
milliseconds |
The start of the millisecond. |
second |
The start of the second. |
minute |
The start of the minute. |
hour |
The start of the hour. |
day |
The start of the day. |
week |
The start of the ISO week (Monday). |
month |
The start of the month. |
quarter |
The start of the quarter. |
year |
The start of the year. |
decade |
The start of the decade. |
century |
The start of the century. |
millennium |
The start of the millennium. |
Return value
The truncated datetime value of the same type as source. If source is not a datetime value, it is interpreted as a DATETIME value and returned as a DATETIME value.
Considerations
- If the value of
precisionis not in the preceding list (for example,weeksormonths), it is treated as an invalid time unit and an error is returned. - The value of
precisionis case-insensitive (for example,MONTH,Month, andmonthare equivalent).
Examples
Truncate the timestamp to the start of the month.
obclient> SELECT DATE_TRUNC('month', TIMESTAMP '2026-03-24 14:30:00') AS d;The result is as follows:
+---------------------+ | d | +---------------------+ | 2026-03-01 00:00:00 | +---------------------+ 1 row in setTruncate to the start of the quarter.
obclient> SELECT DATE_TRUNC('quarter', TIMESTAMP '2026-08-15 09:00:00') AS d;The result is as follows:
+---------------------+ | d | +---------------------+ | 2026-07-01 00:00:00 | +---------------------+ 1 row in setTruncate to the start of the ISO week (Monday).
obclient> SELECT DATE_TRUNC('week', TIMESTAMP '2026-04-29 10:00:00') AS d;The result is as follows:
+---------------------+ | d | +---------------------+ | 2026-04-27 00:00:00 | +---------------------+ 1 row in setReturn the
DATEvalue in its original type (it is not forcibly converted toDATETIME).obclient> SELECT DATE_TRUNC('month', DATE '2026-03-24') AS d;The result is as follows:
+------------+ | d | +------------+ | 2026-03-01 | +------------+ 1 row in set
