DATE_DIFF
All functions > DATE_AND_TIME > DATE_DIFF
Returns the difference between the two given timestamps in the given unit.
Syntax
DATE_DIFF(timestamp1, timestamp2, unit)
sql
Arguments
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') |
Returns
The number of units between the two timestamps as BIGINT
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