Sub topic 4
Adding and subtracting days
This chapter assumes you can write basic SELECT queries with a WHERE clause, that you know the DUAL table, and that you have read the earlier chapters on the DATE type and SYSDATE. Date arithmetic with plain + and - is the foundation for the month functions later in the tutorial.
Dates behave like numbers of days
Oracle lets you apply + and - to a DATE value. The unit is always one day. A whole number moves the date by whole days. A fraction moves it within the day, because a DATE stores a time of day as well as a calendar day.
Only some combinations are allowed:
| Expression | Result type | Meaning |
|---|---|---|
| DATE + number | DATE | A later date |
| DATE - number | DATE | An earlier date |
| DATE - DATE | NUMBER | Days between, possibly fractional |
| DATE + DATE | Error (ORA-00975) | Not meaningful, so it is rejected |
Adding days and fractions of a day
The next query moves a date forward by a whole day and by one hour. It also subtracts two literal dates:
SELECT TRUNC(SYSDATE) + 1 AS tomorrow_midnight,
SYSDATE + 1/24 AS in_one_hour,
DATE '2024-03-01' - DATE '2024-02-01' AS days_between
FROM dual;TRUNC(SYSDATE) + 1first drops the time of day, so the result is midnight at the start of tomorrow.SYSDATE + 1/24keeps the time of day and adds one hour.days_betweenis 29, because February 2024 has 29 days.
To add smaller units, divide by the number of units in a day:
| Unit | Fraction of a day |
|---|---|
| 1 hour | 1/24 |
| 1 minute | 1/1440 |
| 1 second | 1/86400 |
Write the fraction as an expression, such as 1/1440. Do not type a rounded decimal like 0.000694. The expression keeps full precision.
SELECT TO_CHAR(
TO_DATE('2024-03-01 23:30:00', 'YYYY-MM-DD HH24:MI:SS') + 30/1440,
'YYYY-MM-DD HH24:MI:SS') AS half_hour_later
FROM dual;This returns 2024-03-02 00:00:00. Adding 30 minutes crossed midnight, and Oracle moved the day, month or year as needed. You never handle carries yourself.
Subtracting days
Subtraction works the same way:
SELECT TRUNC(SYSDATE) - 7 AS a_week_ago_midnight,
SYSDATE - 90 AS ninety_days_ago_same_time
FROM dual;A DATE has no time zone and no daylight saving rule. One day is always exactly 24 hours in this arithmetic, even on a day when local clocks change.
Subtracting two dates
DATE - DATE returns a plain NUMBER of days. Its fractional part is the leftover time of day.
SELECT TO_DATE('2024-03-01 18:00', 'YYYY-MM-DD HH24:MI')
- TO_DATE('2024-03-01 06:00', 'YYYY-MM-DD HH24:MI') AS diff_days
FROM dual;The result is .5, which is half a day (12 hours). SQL*Plus and many clients display it without a leading zero.
Multiply the result to change units:
- Days times 24 gives hours.
- Days times 1440 gives minutes.
- Days times 86400 gives seconds.
The order of operands matters
The sign tells you which date is later. later - earlier is positive, and earlier - later is negative. If a report shows negative durations, check the operand order first.
Counting inclusively
DATE '2024-03-10' - DATE '2024-03-01' is 9. That is the number of day boundaries crossed. If you want to count both the first and last day, as in "the booking covers 10 calendar days", add 1:
SELECT DATE '2024-03-10' - DATE '2024-03-01' + 1 AS inclusive_days
FROM dual;Decide which meaning your business rule needs and write it down in a comment. Off-by-one errors here are common.
Breaking a difference into days, hours, minutes and seconds
A frequent request is a duration shown as "2 days 2 hours 30 minutes 30 seconds". You can get this from the day difference, but you must avoid a trap.
Oracle's NUMBER type is decimal, but values such as 181830/86400 have infinite decimal expansions. Oracle stores about 38 significant digits. Multiplying back by 86400 can therefore give 181829.99999999999999999999999999999999 instead of 181830. A TRUNC on that value is off by one second. Round to whole seconds first, then use integer arithmetic.
SELECT TRUNC(total_sec / 86400) AS days,
TRUNC(MOD(total_sec, 86400) / 3600) AS hours,
TRUNC(MOD(total_sec, 3600) / 60) AS minutes,
MOD(total_sec, 60) AS seconds
FROM (
SELECT ROUND((end_dt - start_dt) * 86400) AS total_sec
FROM (
SELECT TO_DATE('2024-03-01 08:15:20', 'YYYY-MM-DD HH24:MI:SS') AS start_dt,
TO_DATE('2024-03-03 10:45:50', 'YYYY-MM-DD HH24:MI:SS') AS end_dt
FROM dual
)
);The inner query computes 181830 total seconds. The outer query returns 2 days, 2 hours, 30 minutes and 30 seconds.
Common errors and gotchas
Adding two dates
SYSDATE + SYSDATE raises ORA-00975. If you wanted an average or midpoint of two dates, add half of their difference to the earlier one:
SELECT DATE '2024-03-01' + (DATE '2024-03-11' - DATE '2024-03-01') / 2 AS midpoint
FROM dual;The result is 2024-03-06.
Treating 30 days as one month
SYSDATE + 30 is 30 days later. It is not one month later. Months have 28 to 31 days, so use ADD_MONTHS when you mean calendar months. The next chapters cover it in detail.
Forgetting the time component
Day arithmetic keeps the time of day. This predicate seems to mean "since seven days ago":
WHERE order_date > SYSDATE - 7It excludes orders placed earlier on the day exactly seven days ago. Compare against midnight if you want whole days:
WHERE order_date >= TRUNC(SYSDATE) - 7The TRUNC chapter and the range-filter chapter return to this.
Mixing DATE and TIMESTAMP
If either operand of a subtraction is a TIMESTAMP, Oracle converts the DATE and returns an INTERVAL DAY TO SECOND, not a NUMBER. For example, SYSTIMESTAMP - SYSDATE does not return a number of days. Multiplying that result by 24 raises an error. Keep both operands the same type, or use CAST(x AS DATE) on the TIMESTAMP when you want numeric day arithmetic. Casting drops the fractional seconds and the time zone.
Implicit conversion of strings
Avoid arithmetic on strings such as '2024-03-01' + 1. Whether this works depends on the session's NLS_DATE_FORMAT, so the same query can succeed in one session and fail in another. Use a date literal (DATE '2024-03-01') or TO_DATE with an explicit format.
Floating fractions are not always exact
1/3 of a day is 8 hours, but Oracle cannot store 0.333... exactly. Expect rounding to the nearest second in some cases. If you need exact steps, use whole seconds (n/86400) or an interval such as NUMTODSINTERVAL(8, 'HOUR').
Practical patterns
Deadline columns:
SELECT order_id,
order_date,
order_date + 14 AS payment_due,
CASE WHEN SYSDATE > order_date + 14 THEN 'OVERDUE' ELSE 'OPEN' END AS status
FROM orders;Age of a record in days, with whole days only:
SELECT order_id,
TRUNC(SYSDATE - order_date) AS age_in_days
FROM orders;Use TRUNC around the whole difference to drop the fractional day. Using TRUNC(SYSDATE) - TRUNC(order_date) instead counts calendar-day boundaries. The two methods differ when the order was placed late in the day.
Exercise
What does DATE '2024-02-28' + 2 return, and what does DATE '2024-03-01' - DATE '2024-02-28' return?
Show answer
The first expression returns 2024-03-01, because 2024 is a leap year and February has a 29th. 28 February plus 1 day is 29 February, and plus 2 days is 1 March. The second expression returns the number 2, since 29 February is counted as a day in between.
Exercise
Write a query that returns the moment 90 minutes after SYSDATE, formatted as 'YYYY-MM-DD HH24:MI:SS'.
Show answer
SELECT TOCHAR(SYSDATE + 90/1440, 'YYYY-MM-DD HH24:MI:SS') AS ninetyminutes_later FROM dual;
Exercise
An order was placed at 2024-05-10 22:00:00. Today is 2024-05-12 at 09:00:00. What does TRUNC(SYSDATE - orderdate) return, and what does TRUNC(SYSDATE) - TRUNC(orderdate) return? Why do they differ?
Show answer
The first returns 1, because the elapsed time is 1 day and 11 hours (1.458... days) and TRUNC drops the fraction. The second returns 2, because it compares the calendar dates 2024-05-12 and 2024-05-10 after both times are removed. The first measures elapsed time, and the second counts midnight boundaries crossed.
Exercise
Why is ROUND((enddt - startdt) 86400) safer than TRUNC((enddt - startdt) 86400) when computing elapsed seconds?
Show answer
A day fraction such as 181830/86400 cannot be stored exactly in a NUMBER, so multiplying it back by 86400 can give a value just below the true whole number of seconds, such as 181829.999.... TRUNC would then return one second too few. ROUND returns the correct whole number, and DATE values never have fractional seconds.