What Is DATEDIFF in SQL?

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.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.
DATEDIFF returns the count of specified date parts (days, months, years, etc.) between two date-time values, excluding the start date. Use it to measure ages, durations, and time gaps across SQL dialects like SQL Server, Snowflake, Redshift, and BigQuery.

What Is DATEDIFF in SQL?

DATEDIFF returns the integer difference between two dates in a chosen unit.

How does DATEDIFF work in plain language?

DATEDIFF counts the number of boundaries crossed for the specified date part between two timestamps, excluding the start date and including the end date.

What is the basic DATEDIFF syntax?

The canonical pattern is DATEDIFF(date_part, start_date, end_date) where date_part is a keyword like day, month, or year.

Which SQL dialects support DATEDIFF?

SQL Server, Snowflake, Redshift, BigQuery, SAP HANA, and Sybase support DATEDIFF natively. MySQL and Postgres require equivalent functions or expression work-arounds.

When should I use DATEDIFF?

Use DATEDIFF for age calculations, churn cohorts, subscription tenure, SLA breach detection, or any metric needing a simple count of time units.

What are common date parts accepted?

Supported parts include microsecond, second, minute, hour, day, week, month, quarter, and year.

How are negative results handled?

DATEDIFF returns a negative integer when end_date precedes start_date, allowing easy lag analysis.

Does DATEDIFF round or truncate?

DATEDIFF always truncates toward zero because it counts whole date part boundaries, not fractional differences.

Example: How many days between order and shipment?

To measure fulfillment speed, subtract order_date from ship_date with day as the part.

Example: How old is a customer in years?

Age in years is DATEDIFF(year, birth_date, CURRENT_DATE); adjust for MySQL/Postgres with their equivalents.

Example: How long has a ticket been open in hours?

For SLA dashboards, use DATEDIFF(hour, created_at, CURRENT_TIMESTAMP) to display live queue duration.

Does DATEDIFF include time zones?

DATEDIFF operates on the literal timestamps provided. Convert to a common time zone first for consistent results.

How do NULLs affect DATEDIFF?

If either date is NULL, DATEDIFF returns NULL, so wrap inputs with COALESCE when defaults are acceptable.

Can I nest DATEDIFF inside other functions?

Yes. DATEDIFF can be used in CASE expressions, window functions, view definitions, and CTEs without restriction.

Best practice: index filtering with DATEDIFF?

Filter on raw dates rather than DATEDIFF results to leverage indexes, then compute DATEDIFF in the select list.

Best practice: parameterize date parts?

Hard-code valid date parts or use parameterized statements with strict enumeration to avoid SQL injection or typos.

How does DATEDIFF compare to TIMESTAMPDIFF?

TIMESTAMPDIFF in MySQL and Presto matches DATEDIFF behavior. Choose the native function for clarity in each dialect.

Edge case: leap years and month ends?

DATEDIFF counts whole boundaries, so DATEDIFF(day,'2024-02-28','2024-03-01') returns 2 even across leap-day adjustments.

Edge case: daylight saving gaps?

DATEDIFF uses calendar boundaries, not absolute seconds, so DST gaps/overlaps do not affect day, month, or year counts.

What performance considerations apply?

DATEDIFF itself is lightweight; performance issues arise only when wrapping indexed columns in the function inside WHERE clauses.

Next steps for learning?

Practice DATEDIFF with varying date parts, combine it with window functions for cohort analysis, and explore Galaxy’s AI copilot for automated query optimization.

Key takeaways

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.

Frequently Asked Questions (FAQs)

What is the default date part in DATEDIFF?

There is no default; you must specify the date part explicitly, such as day or month.

Does DATEDIFF work with timestamps that include time?

Yes, time components are honored for smaller date parts like hour and minute; for larger parts, only calendar boundaries matter.

How do I compare DATEDIFF across different SQL engines?

Check each engine’s documentation for supported date parts and argument order. Most follow (part,start,end), but validate before porting.

Why is my DATEDIFF returning huge numbers?

A reversed argument order or mismatched time zones often inflates results. Ensure start_date precedes end_date and convert zones.

Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Check out our other posts!

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