DATE_TRUNC
All functions > DATE AND TIME > DATE_TRUNC
Returns the given timestamp truncated to the given unit.
Signatures
Returns: The timestamp truncated to the specified unit
DATE_TRUNC(timestamp: TIMESTAMP, unit: VARCHAR) → TIMESTAMP sql
| Parameter | Type | Required | Description |
|---|---|---|---|
timestamp | TIMESTAMP | Yes | The TIMESTAMP to truncate |
unit | VARCHAR | Yes | The time unit to truncate to (e.g., 'day', 'hour', 'month') |
Notes
- Truncates timestamp to the beginning of the specified unit
- Common units: 'second', 'minute', 'hour', 'day', 'week', 'month', 'quarter', 'year'
- Useful for grouping timestamps into time buckets
- Time components smaller than the unit are set to their minimum values
- Example: DATE_TRUNC(timestamp, 'day') returns midnight of that day
Examples
FeatureQL
SELECT
f1 := DATE_TRUNC(FROM_ISO8601_TIMESTAMP('2024-03-15T10:30:45'), 'day'), -- Truncate to start of day
f2 := DATE_TRUNC(FROM_ISO8601_TIMESTAMP('2024-03-15T10:30:45'), 'hour'), -- Truncate to start of hour
f3 := DATE_TRUNC(FROM_ISO8601_TIMESTAMP('2024-03-15T10:30:45'), 'month') -- Truncate to start of month
;Result
| f1 TIMESTAMP | f2 TIMESTAMP | f3 TIMESTAMP |
|---|---|---|
| 2024-03-15 | 2024-03-15T10:00:00 | 2024-03-01 |
On this page