Subtract one DATE or TIMESTAMP from another (or use MONTHS_BETWEEN, NUMTODSINTERVAL, EXTRACT) to return elapsed days, months, or smaller time units.
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.
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;
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;
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;
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;
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.
1. Comparing VARCHAR dates—always TO_DATE/TO_TIMESTAMP first.
2. Ignoring fractional days—use ROUND or TRUNC explicitly.
Use a calendar table or combine NEXT_DAY with CASE logic to skip weekends and holidays, then aggregate.
Yes. Multiply the day difference by 24 or wrap the NUMBER in NUMTODSINTERVAL(n,'HOUR') to return an INTERVAL value.
Cast both operands to TIMESTAMP WITH TIME ZONE. Oracle normalizes the values before subtraction, giving an accurate interval regardless of daylight-saving shifts.
No. MONTHS_BETWEEN returns a decimal. Apply ROUND or TRUNC to control precision.