DATE_DIFF()
All functions > DATE AND TIME > DATE_DIFF()
Returns the difference between start and end in the given unit (end − start). Matches the SQL/DuckDB argument order.
Signatures
Returns: Number of units between start and end (end − start)
DATE_DIFF(unit: VARCHAR, start: DATE | TIMESTAMP, end: DATE | TIMESTAMP) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
unit | VARCHAR | Yes | Time unit for the result (e.g. 'day', 'hour') |
start | `DATE | TIMESTAMP` | Yes |
end | `DATE | TIMESTAMP` | Yes |
Notes
- Result equals end − start in the given unit
- Positive when end is after start, negative otherwise
- DATE and TIMESTAMP inputs can be mixed freely
- Common units: 'second', 'minute', 'hour', 'day', 'week', 'month', 'year'
- Argument order matches DuckDB's native DATE_DIFF(part, startdate, enddate)
- Use DATE_SUBTRACT for the chaining-friendly (base, subtrahend, unit) order
Examples
Date inputs
FeatureQL
SELECT
f1 := DATE_DIFF('day', DATE '2024-03-15', DATE '2024-03-22'), -- Days between two dates
f2 := DATE_DIFF('month', DATE '2024-03-15', DATE '2024-06-01') -- Months between two dates
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| 7 | 3 |
Mixed DATE and TIMESTAMP inputs
FeatureQL
SELECT
f1 := DATE_DIFF('day', TIMESTAMP '2024-03-15 10:00:00', DATE '2024-06-01') -- Days from a timestamp to a date boundary
;Result
| f1 BIGINT |
|---|
| 78 |
Timestamp inputs
FeatureQL
SELECT
f1 := DATE_DIFF('day', TIMESTAMP '2024-03-15 10:00:00', TIMESTAMP '2024-03-22 10:00:00'), -- Difference in days
f2 := DATE_DIFF('hour', TIMESTAMP '2024-03-15 10:00:00', TIMESTAMP '2024-03-15 13:00:00') -- Difference in hours
;Result
| f1 BIGINT | f2 BIGINT |
|---|---|
| 7 | 3 |