DATE_DIFF
All functions > DATE AND TIME > DATE_DIFF
Returns the difference between the two given timestamps in the given unit.
Signatures
Returns: The number of units between the two timestamps as BIGINT
DATE_DIFF(timestamp1: TIMESTAMP, timestamp2: TIMESTAMP, unit: VARCHAR) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
timestamp1 | TIMESTAMP | Yes | The start timestamp |
timestamp2 | TIMESTAMP | Yes | The end timestamp |
unit | VARCHAR | Yes | The time unit for the difference (e.g., 'day', 'hour') |
Notes
- Calculates difference between two timestamps
- Result is timestamp2 - timestamp1 in the specified unit
- Common units: 'second', 'minute', 'hour', 'day', 'week', 'month', 'year'
- Positive if timestamp2 is after timestamp1, negative otherwise
- Useful for calculating durations and time elapsed
Examples
FeatureQL
SELECT
f1 := DATE_DIFF(TIMESTAMP '2024-03-15 10:00:00', TIMESTAMP '2024-03-22 10:00:00', 'day'), -- Difference in days
f2 := DATE_DIFF(TIMESTAMP '2024-03-15 10:00:00', TIMESTAMP '2024-03-15 13:00:00', 'hour') -- Difference in hours
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| 7 | 3 |
On this page