How to Calculate Date Difference in Oracle

Galaxy Glossary

How do I calculate the difference between two dates in Oracle SQL?

Subtract one DATE or TIMESTAMP from another (or use MONTHS_BETWEEN, NUMTODSINTERVAL, EXTRACT) to return elapsed days, months, or smaller time units.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Description

Why does Oracle not have a DATEDIFF function?

Oracle supports direct arithmetic on DATE and TIMESTAMP types, so a dedicated DATEDIFF is unnecessary. Subtracting one date from another returns a NUMBER of days. Functions like MONTHS_BETWEEN, NUMTODSINTERVAL, and EXTRACT help you derive months, years, hours, minutes, or seconds without custom code.

How do I get days between two DATE columns?

Subtract the columns; the result is days, including fractional parts for hours and minutes.

SELECT o.id,
o.order_date,
c.created_at,
o.order_date - c.created_at AS days_since_signup
FROM Orders o
JOIN Customers c ON c.id = o.customer_id;

How can I find months or years between dates?

Use MONTHS_BETWEEN for months. Divide by 12 or TRUNC for whole years.

SELECT MONTHS_BETWEEN(o.order_date, c.created_at) AS months_gap,
TRUNC(MONTHS_BETWEEN(o.order_date, c.created_at)/12) AS years_gap
FROM Orders o
JOIN Customers c ON c.id = o.customer_id;

How do I calculate hours, minutes, or seconds?

Multiply the day difference or convert with NUMTODSINTERVAL.

SELECT o.id,
ROUND((o.order_date - c.created_at) * 24) AS hours_gap
FROM Orders o
JOIN Customers c ON c.id = o.customer_id;

What is the safest way to ignore time parts?

Wrap both operands in TRUNC to compare midnight-to-midnight.

SELECT TRUNC(o.order_date) - TRUNC(c.created_at) AS full_days
FROM Orders o
JOIN Customers c ON c.id = o.customer_id;

Best practices for accurate date differences

Store timestamps in UTC, cast to TIMESTAMP WITH TIME ZONE when zones matter, and document whether fractional days are acceptable. Use EXTRACT when only one component is needed.

Common mistakes to avoid

1. Comparing VARCHAR dates—always TO_DATE/TO_TIMESTAMP first.
2. Ignoring fractional days—use ROUND or TRUNC explicitly.

How can I count business days only?

Use a calendar table or combine NEXT_DAY with CASE logic to skip weekends and holidays, then aggregate.

Why How to Calculate Date Difference in Oracle is important

How to Calculate Date Difference in Oracle Example Usage


SELECT o.id,
       c.name,
       o.order_date,
       o.order_date - c.created_at                AS days_since_signup,
       MONTHS_BETWEEN(o.order_date, c.created_at) AS months_since_signup,
       ROUND((o.order_date - c.created_at)*24)    AS hours_since_signup
FROM   Orders   o
JOIN   Customers c ON c.id = o.customer_id
WHERE  o.order_date >= SYSDATE - 30;

How to Calculate Date Difference in Oracle Syntax


-- Days (default)
<code>date1 - date2</code>

-- Months
<code>MONTHS_BETWEEN(date1, date2)</code>

-- Years (whole)
<code>TRUNC(MONTHS_BETWEEN(date1, date2)/12)</code>

-- Hours, Minutes, Seconds via interval
<code>NUMTODSINTERVAL(number, 'DAY'|'HOUR'|'MINUTE'|'SECOND')</code>

-- Extract single component
<code>EXTRACT(DAY FROM (date2 - date1) DAY TO SECOND)</code>

Common Mistakes

Frequently Asked Questions (FAQs)

Can I get the date difference in hours directly?

Yes. Multiply the day difference by 24 or wrap the NUMBER in NUMTODSINTERVAL(n,'HOUR') to return an INTERVAL value.

How do I include time zone when comparing dates?

Cast both operands to TIMESTAMP WITH TIME ZONE. Oracle normalizes the values before subtraction, giving an accurate interval regardless of daylight-saving shifts.

Is MONTHS_BETWEEN rounded?

No. MONTHS_BETWEEN returns a decimal. Apply ROUND or TRUNC to control precision.

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
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.