DATE_DIFF / DATETIME_DIFF / TIMESTAMP_DIFF return the numeric interval between two dates, datetimes, or timestamps in BigQuery.
Use the DATE_DIFF, DATETIME_DIFF, or TIMESTAMP_DIFF functions. They subtract a start value from an end value and return the result in the unit you choose—seconds, minutes, hours, days, weeks, months, quarters, or years.
Pass three arguments: DATE_DIFF(end_date, start_date, DATE_PART)
. DATE_PART
is a string literal such as 'DAY'
or 'MONTH'
. The function always returns an INT64.
Match the data type of your columns. For DATE columns use DATE_DIFF
. For combined date-time use DATETIME_DIFF
. For timezone-aware timestamps, use TIMESTAMP_DIFF
.
Yes. Join Orders
to itself, sort by order_date
, and call DATE_DIFF
on consecutive orders. This reveals how many days pass between purchases.
Always filter out NULL start or end values. DATE_DIFF on NULL returns NULL, which may hide errors in downstream metrics.
Store units in a comment or alias column so analysts know whether a metric is in days or months. Consistency prevents misinterpretation during reporting.
No. DATE_DIFF subtracts start_date
from end_date
. The day count excludes the start date and includes the end date.
No. All *_DIFF functions return INT64. To get fractions, calculate seconds with TIMESTAMP_DIFF then divide by a float.