How to work out date differences in SQL with datediff
Temporal data is at the heart of many data engineering and analytics tasks—whether it’s measuring user engagement, tracking system uptime, or calculating financial returns over time. One of the fundamental operations in time-based analysis is determining the difference between two dates or timestamps. This is where the DATEDIFF
function comes into play.
Learn SQL online
What is DATEDIFF ?
DATEDIFF
is a built-in function available in many SQL dialects and data processing platforms (such as T-SQL, MySQL, Snowflake, and others) that calculates the difference between two dates in terms of a specified date part, such as days, months, or years.
General Syntax
While the exact syntax can vary by system, a common pattern is:
DATEDIFF(datepart, start_date, end_date)
datepart
: The unit of time to measure the difference (e.g.,day
,month
,year
).start_date
: The beginning of the interval.end_date
: The end of the interval.
The function returns an integer indicating the number of datepart
boundaries crossed between the two dates.
Example in SQL Server (T-SQL)
SELECT DATEDIFF(day, '2023-01-01', '2023-01-10') AS DayDifference;
he result here is 9
because there are nine full days between January 1st and January 10th.
Key Points to Understand
Direction of Difference
DATEDIFF
returns a signed integer. If the end_date
is earlier than the start_date
, the result is negative.
SELECT DATEDIFF(day, '2023-01-10', '2023-01-01');
Unit-Specific Behavior
Different units may behave differently, especially with months and years:
SELECT DATEDIFF(month, '2023-01-31', '2023-02-01');
-- Output: 1 (even though only one day passed)
This result reflects that the month boundary has been crossed.
Time-Agnostic in Some Implementations
In many SQL dialects, DATEDIFF
ignores time components and compares only the date part unless specified otherwise. However, platforms like PostgreSQL, which uses AGE()
or arithmetic on INTERVAL
, treat time more granularly.
Platform Differences
Platform | Function | Notes |
---|---|---|
T-SQL | DATEDIFF | Accepts datepart as the first argument |
MySQL | DATEDIFF | Returns days only; no datepart specification |
PostgreSQL | No DATEDIFF | Use AGE() or subtract dates directly |
Snowflake | DATEDIFF | Syntax: DATEDIFF('unit', start, end) |
Common Use Cases
User churn analysis: Time between last login and current date.
Subscription duration: Days or months since a user subscribed.
Lag in processes: Time difference between stages in a data pipeline.
Event comparisons: How long between order placed and order shipped.
The DATEDIFF
function is a versatile and essential tool for temporal data manipulation. However, it’s important to understand its implementation nuances across platforms—especially how boundaries are calculated and whether time components are considered.
When working with cross-platform data pipelines or transforming SQL logic to Spark or Pandas, be mindful of these differences to ensure accuracy in your date-based calculations.
Please see the other SQL video learning articles below as part of this learning SQL online course video.
If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY.