SQL’s DATEDIFF function returns the integer difference between two date-time values in the unit you specify (days, months, years, etc.). Supported in SQL Server, Snowflake, Redshift, BigQuery, and more, DATEDIFF answers “How many units separate DateA and DateB?” It excludes the start date, uses the calendar boundaries of the chosen unit, and is ideal for age, retention, and duration analysis.
DATEDIFF returns the integer difference between two dates in a chosen unit.
DATEDIFF counts the number of boundaries crossed for the specified date part between two timestamps, excluding the start date and including the end date.
The canonical pattern is DATEDIFF(date_part, start_date, end_date)
where date_part
is a keyword like day
, month
, or year
.
SQL Server, Snowflake, Redshift, BigQuery, SAP HANA, and Sybase support DATEDIFF natively. MySQL and Postgres require equivalent functions or expression work-arounds.
Use DATEDIFF for age calculations, churn cohorts, subscription tenure, SLA breach detection, or any metric needing a simple count of time units.
Supported parts include microsecond
, second
, minute
, hour
, day
, week
, month
, quarter
, and year
.
DATEDIFF returns a negative integer when end_date
precedes start_date
, allowing easy lag analysis.
DATEDIFF always truncates toward zero because it counts whole date part boundaries, not fractional differences.
To measure fulfillment speed, subtract order_date
from ship_date
with day
as the part.
Age in years is DATEDIFF(year, birth_date, CURRENT_DATE)
; adjust for MySQL/Postgres with their equivalents.
For SLA dashboards, use DATEDIFF(hour, created_at, CURRENT_TIMESTAMP)
to display live queue duration.
DATEDIFF operates on the literal timestamps provided. Convert to a common time zone first for consistent results.
If either date is NULL, DATEDIFF returns NULL, so wrap inputs with COALESCE
when defaults are acceptable.
Yes. DATEDIFF can be used in CASE
expressions, window functions, view definitions, and CTEs without restriction.
Filter on raw dates rather than DATEDIFF results to leverage indexes, then compute DATEDIFF in the select list.
Hard-code valid date parts or use parameterized statements with strict enumeration to avoid SQL injection or typos.
TIMESTAMPDIFF in MySQL and Presto matches DATEDIFF behavior. Choose the native function for clarity in each dialect.
DATEDIFF counts whole boundaries, so DATEDIFF(day,'2024-02-28','2024-03-01')
returns 2 even across leap-day adjustments.
DATEDIFF uses calendar boundaries, not absolute seconds, so DST gaps/overlaps do not affect day
, month
, or year
counts.
DATEDIFF itself is lightweight; performance issues arise only when wrapping indexed columns in the function inside WHERE clauses.
Practice DATEDIFF with varying date parts, combine it with window functions for cohort analysis, and explore Galaxy’s AI copilot for automated query optimization.
DATEDIFF counts whole date parts between two timestamps, supports many SQL engines, and excels at age and duration metrics when used with indexing best practices.
There is no default; you must specify the date part explicitly, such as day
or month
.
Yes, time components are honored for smaller date parts like hour
and minute
; for larger parts, only calendar boundaries matter.
Check each engine’s documentation for supported date parts and argument order. Most follow (part,start,end)
, but validate before porting.
A reversed argument order or mismatched time zones often inflates results. Ensure start_date
precedes end_date
and convert zones.