DATEDIFF returns the number of specified date parts between two dates or timestamps in Amazon Redshift.
DATEDIFF quickly calculates elapsed time, letting you derive customer tenure, order processing days, or inventory aging without manual arithmetic.
Redshift counts the number of boundaries of a chosen datepart (day, week, month, etc.) that occur between startdate
and enddate
. It supports DATE, TIMESTAMP, and TIMESTAMPTZ.
Use DATEDIFF(datepart, startdate, enddate)
. See the full syntax section below for parameters and allowed date parts.
Join Orders
to a shipping table (or use an in-table ship_date) and apply DATEDIFF('day', order_date, ship_date)
to measure fulfillment speed.
Yes. DATEDIFF('month', created_at, CURRENT_DATE)
calculates the number of month boundaries crossed since the customer signed up.
No. It counts boundary crossings only. From Jan-31 to Feb-01 returns 1 day—even though the actual duration is 1 day. For hours, minutes, or seconds, boundaries behave the same.
Choose the finest necessary datepart. Filter nulls before calling DATEDIFF. Store timestamps in UTC to avoid DST surprises.
Misordered parameters: DATEDIFF('day', end, start)
flips the sign. Always pass startdate
first.
Wrong datepart: Using 'week'
when you need exact days skips residual days. Pick 'day'
for precise day counts.
Syntax: DATEDIFF(part, start, end)
. Returns INTEGER. Supports DATE | TIMESTAMP | TIMESTAMPTZ. Typical parts: year, quarter, month, week, day, hour, minute, second.
No. It counts the number of boundaries crossed, so Jan-01 to Jan-01 returns 0 days.
Yes. enddate - startdate
returns an INTERVAL, but DATEDIFF gives an INTEGER, which is often easier for aggregations.
No. The finest supported part is 'second'
. Use plain subtraction and extract if microsecond precision is required.