DATE_TRUNC
Truncates a datetime value to the specified time unit.
Syntaxโ
DATE_TRUNC(<datetime>, <time_unit>)
DATE_TRUNC(<time_unit>, <datetime>)
Starting from VeloDB Core 4.0.5, DATE_TRUNC supports both argument orders shown above. The VeloDB Core 4.1 series also supports both. On earlier VeloDB Core 4.0 releases, use DATE_TRUNC(<datetime>, <time_unit>), with the time unit as the second argument.
Parametersโ
| Parameter | Description |
|---|---|
<datetime> | A valid date expression. |
<time_unit> | The time unit to truncate to. It must be a string constant. Valid values are second, minute, hour, day, week, month, quarter, and year. |
Return valueโ
The datetime value truncated to the specified time unit.
Examplesโ
Both forms return the same result:
SELECT DATE_TRUNC('2025-04-28 12:12:12', 'year') AS truncated;
+---------------------+
| truncated |
+---------------------+
| 2025-01-01 00:00:00 |
+---------------------+
SELECT DATE_TRUNC('year', '2025-04-28 12:12:12') AS truncated;
+---------------------+
| truncated |
+---------------------+
| 2025-01-01 00:00:00 |
+---------------------+
select date_trunc('2010-12-02 19:28:30', 'second');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'second') |
+-------------------------------------------------+
| 2010-12-02 19:28:30 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'minute');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'minute') |
+-------------------------------------------------+
| 2010-12-02 19:28:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'hour');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'hour') |
+-------------------------------------------------+
| 2010-12-02 19:00:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'day');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'day') |
+-------------------------------------------------+
| 2010-12-02 00:00:00 |
+-------------------------------------------------+
select date_trunc('2023-4-05 19:28:30', 'week');
+-------------------------------------------+
| date_trunc('2023-04-05 19:28:30', 'week') |
+-------------------------------------------+
| 2023-04-03 00:00:00 |
+-------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'month');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'month') |
+-------------------------------------------------+
| 2010-12-01 00:00:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'quarter');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'quarter') |
+-------------------------------------------------+
| 2010-10-01 00:00:00 |
+-------------------------------------------------+
select date_trunc('2010-12-02 19:28:30', 'year');
+-------------------------------------------------+
| date_trunc('2010-12-02 19:28:30', 'year') |
+-------------------------------------------------+
| 2010-01-01 00:00:00 |
+-------------------------------------------------+