How to Use dateDiff in ClickHouse

Galaxy Glossary

How do I calculate the difference between two dates in ClickHouse?

dateDiff returns the elapsed time between two Date/DateTime values in units like day, hour, or month.

Sign up for the latest in SQL knowledge from the Galaxy Team!

Description

What does dateDiff do in ClickHouse?

dateDiff subtracts a start date from an end date and returns the integer count of the requested time unit (second, minute, hour, day, week, month, quarter, or year). It works with Date, Date32, and DateTime types.

Why would I use dateDiff in ecommerce analytics?

Use dateDiff to measure customer latency—days between signup and first order, hours between order and shipping, or months between repeat purchases.These metrics uncover churn risk and growth opportunities.

How do I write the dateDiff syntax?

Place the unit first, then the two date expressions: dateDiff('unit', start_date, end_date). ClickHouse returns a signed integer; negative results mean the end date precedes the start.

Can I mix Date and DateTime?

Yes.ClickHouse casts Date to midnight of that day when mixed with DateTime, letting you compare at any granularity.

Example – days between customer signup and first order

The query below joins Customers with their earliest Orders and calculates the days waited before purchase.

SELECT c.id,
c.name,
dateDiff('day', c.created_at, MIN(o.order_date)) AS days_to_first_order
FROM Customers AS c
JOIN Orders AS o ON o.customer_id = c.id
GROUP BY c.id, c.name;

What units are supported?

"second", "minute", "hour", "day", "week", "month", "quarter", and "year" are valid.Capitalization is ignored.

Best practices for dateDiff

Always index or use partition keys on the date columns you filter. Use consistent time zones—store in UTC and convert in the final SELECT to avoid off-by-one errors around DST changes.

Common mistakes and fixes

Passing unit as identifier

Wrong: dateDiff(day, …). The unit must be a single-quoted string. Fix: dateDiff('day', …).

Reversing start and end dates

ClickHouse returns negative values if end_date < start_date.Swap arguments or wrap the result in abs() when you only need magnitude.

Related ClickHouse date functions

Complement dateDiff with dateAdd for cohort tracking, toStartOfWeek for grouping, and date_trunc for bucketing time series.

.

Why How to Use dateDiff in ClickHouse is important

How to Use dateDiff in ClickHouse Example Usage


-- Days between first and second purchase
WITH first_orders AS (
  SELECT customer_id,
         MIN(order_date) AS first_order_date,
         anyHeavy(id) AS first_order_id
  FROM Orders
  GROUP BY customer_id
), second_orders AS (
  SELECT customer_id,
         MIN(order_date) AS second_order_date
  FROM Orders
  WHERE order_date > first_order_date
  GROUP BY customer_id
)
SELECT c.id,
       dateDiff('day', f.first_order_date, s.second_order_date) AS days_between_orders
FROM Customers AS c
JOIN first_orders AS f ON f.customer_id = c.id
JOIN second_orders AS s ON s.customer_id = c.id;

How to Use dateDiff in ClickHouse Syntax


dateDiff('unit', start_date, end_date)

unit          – String literal "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year"
start_date    – Any Date/DateTime expression. Typically a column like Customers.created_at.
end_date      – Any Date/DateTime expression. Typically NOW() or Orders.order_date.

Example (hours between order and last status update):
SELECT dateDiff('hour', o.order_date, o.updated_at) AS hours_open
FROM Orders AS o;

Common Mistakes

Frequently Asked Questions (FAQs)

Does dateDiff include the start day?

No. dateDiff counts boundaries crossed. From 2023-01-01 to 2023-01-02 returns 1 day.

Is dateDiff affected by time zones?

ClickHouse treats inputs as they are stored. For consistent results, store all timestamps in UTC or specify time zones explicitly.

Can I use expressions as arguments?

Yes. Pass functions like NOW() or dateAdd() as start or end parameters to build dynamic date ranges.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
BauHealth Logo
Truvideo Logo