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

ParameterTypeRequiredDescription
timestamp1TIMESTAMPYesThe start timestamp
timestamp2TIMESTAMPYesThe end timestamp
unitVARCHARYesThe 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

Examples

Last update at: 2025/10/13 10:23:46