DATEDIFF returns the number of specified date or time parts between two dates or timestamps.
DATEDIFF counts how many complete date or time parts (days, weeks, months, etc.) exist between a start and end value. It supports DATE, TIME, TIMESTAMP, and TIMESTAMP_TZ types.
DATEDIFF( <date_or_time_part>, <start_expr>, <end_expr> )
<date_or_time_part> can be year, quarter, month, week, day, hour, minute, second, etc. start_expr
and end_expr
are the two dates or timestamps you want to compare.
SELECT c.id,
DATEDIFF(day, c.created_at, MIN(o.order_date)) AS days_to_first_order
FROM Customers c
JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id;
The query returns the number of days each customer waited before placing their first order.
SELECT o1.id AS first_order,
o2.id AS second_order,
DATEDIFF(month, o1.order_date, o2.order_date) AS months_between
FROM Orders o1
JOIN Orders o2 ON o2.customer_id = o1.customer_id
WHERE o2.order_date > o1.order_date;
This pattern finds months between any two orders from the same customer.
For TIMESTAMP_TZ values, Snowflake converts both inputs to UTC before calculation, ensuring accurate differences across zones.
DATE
and timestamps in TIMESTAMP_TZ
to avoid implicit casts.DATEDIFF returns negative numbers if start_expr > end_expr
. Swap arguments or wrap with ABS() when you only need magnitude.
DATEDIFF counts whole units only. To include partial days, switch to TIMEDIFF or subtract timestamps and divide by seconds.
Use DATEADD
to shift dates forward or backward, and DATEDIFF
together for cohort analysis.
If either argument is NULL, the function returns NULL. Use COALESCE() to set defaults.
Performance is comparable, but timestamp math returns seconds. DATEDIFF is clearer when you need calendar units.
DATEDIFF can’t skip weekends. Join a calendar table that flags business days, then count rows between the two dates.