Sub topic 11
Rolling Date Windows in `WHERE` Clauses
This chapter assumes that you can write basic SELECT queries and use WHERE conditions. It also assumes that you know an Oracle DATE value includes both a calendar date and a time down to seconds.
A rolling date window is a range that moves as time passes. For example, a report might show orders from the previous three calendar months through today. The safest pattern is to use an inclusive lower boundary and an exclusive upper boundary:
column >= lower_boundary
AND column < upper_boundaryThe exclusive upper boundary prevents time values at the end of a day from being missed.
Include all of today
Suppose orders.order_date is an Oracle DATE. This query returns orders from midnight three months ago through the end of today:
SELECT order_id,
order_date
FROM orders
WHERE order_date >= ADD_MONTHS(TRUNC(SYSDATE), -3)
AND order_date < TRUNC(SYSDATE) + 1;The expressions work as follows:
| Expression | Meaning |
|---|---|
| `SYSDATE` | The database server's current date and time |
| `TRUNC(SYSDATE)` | Today at midnight |
| `ADD_MONTHS(TRUNC(SYSDATE), -3)` | Midnight on the date three calendar months ago |
| `TRUNC(SYSDATE) + 1` | Midnight tomorrow |
If today is June 15 at 14:30, the lower boundary is March 15 at 00:00, and the upper boundary is June 16 at 00:00. An order on June 15 at 23:59:59 is included. An order on June 16 at 00:00:00 is excluded.
Calendar months versus elapsed days
ADD_MONTHS measures calendar months. It does not mean a fixed number of days. For example, subtracting three months from May 31 can produce February 28 or February 29, depending on the year and Oracle's end-of-month rule.
If the requirement is instead “the previous 90 times 24 hours,” subtract days from SYSDATE:
SELECT order_id,
order_date
FROM orders
WHERE order_date >= SYSDATE - 90
AND order_date < SYSDATE;This query uses the current time for both boundaries. It excludes future orders and includes only the preceding 90 days.
Use the requirement to choose the expression:
| Requirement | Lower boundary |
|---|---|
| Previous three calendar months, including today | `ADD_MONTHS(TRUNC(SYSDATE), -3)` |
| Previous 90 elapsed days | `SYSDATE - 90` |
| Current calendar month | `TRUNC(SYSDATE, 'MM')` |
Preserve index-friendly predicates
Avoid wrapping the column in TRUNC when a normal index on that column should support the search:
-- Usually less suitable for a normal index on order_date
WHERE TRUNC(order_date) = TRUNC(SYSDATE)The expression applies a function to every order_date value before comparing it. Instead, express the day as a range:
SELECT order_id,
order_date
FROM orders
WHERE order_date >= TRUNC(SYSDATE)
AND order_date < TRUNC(SYSDATE) + 1;The column remains unmodified. Oracle can often use an ordinary index on order_date more effectively. Always confirm the actual plan for a production query, because table size, statistics, and other predicates also affect the optimizer's choice.
Use explicit boundaries for reports
For repeatable reports, calculate a boundary once and reuse it. A bind variable is preferable when an application supplies the reporting date:
SELECT order_id,
order_date
FROM orders
WHERE order_date >= ADD_MONTHS(TRUNC(:report_date), -3)
AND order_date < TRUNC(:report_date) + 1;Here, :report_date is a bind variable. The report can run for a chosen business date instead of changing while the query executes.
Exercise
Write a query that returns payments from the current calendar month, including all times today but excluding the first instant of next month.
Show answer
SELECT paymentid, paymentdate FROM payments WHERE paymentdate >= TRUNC(SYSDATE, 'MM') AND paymentdate < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1);