Sub topic 12
Companion Functions: LAST_DAY, NEXT_DAY, and TRUNC with Format Models
Assume you can write basic SELECT statements, use WHERE, and understand that an Oracle DATE stores both a calendar date and a time of day. This chapter focuses on functions that find calendar boundaries and recurring weekdays.
Finding the end of a month with LAST_DAY
LAST_DAY(date_value) returns a DATE for the last calendar day of the month containing date_value.
SELECT LAST_DAY(DATE '2024-02-15') AS month_end,
LAST_DAY(DATE '2024-02-29') AS same_month_end,
LAST_DAY(DATE '2024-03-01') AS next_month_end
FROM dual;The results are February 29, February 29, and March 31. Oracle applies the session's NLS_CALENDAR setting when it decides what a month means.
If you need midnight at the end of the month, remove the time portion explicitly:
SELECT TRUNC(LAST_DAY(SYSDATE)) AS last_day_at_midnight
FROM dual;This is useful when constructing inclusive date ranges. However, an exclusive upper boundary is often safer:
SELECT order_id, order_date
FROM orders
WHERE order_date >= TRUNC(SYSDATE, 'MM')
AND order_date < ADD_MONTHS(TRUNC(SYSDATE, 'MM'), 1);This query returns every order in the current month, including orders created at any time on the last day.
Finding a weekday with NEXT_DAY
NEXT_DAY(date_value, weekday_name) returns the first named weekday after the supplied date. The weekday name is interpreted using the session's date language.
SELECT NEXT_DAY(DATE '2024-04-01', 'MONDAY') AS next_monday,
NEXT_DAY(DATE '2024-04-01', 'FRIDAY') AS next_friday
FROM dual;April 1, 2024 was a Monday. Therefore, the next Monday is April 8, not April 1. NEXT_DAY searches forward and does not return the input date when it already has the requested weekday.
The text value depends on NLS_DATE_LANGUAGE. A session configured for another language might not accept MONDAY. Check the database and session conventions before putting weekday names into portable application SQL. NEXT_DAY also retains the time-of-day portion of its date argument, so apply TRUNC when you need a midnight boundary.
Removing time and choosing a calendar boundary with TRUNC
TRUNC(date_value) removes the time portion and returns midnight on the same day. A second argument selects a larger boundary.
SELECT TRUNC(DATE '2024-05-17' + 15/24) AS day_start,
TRUNC(DATE '2024-05-17', 'MM') AS month_start,
TRUNC(DATE '2024-05-17', 'YYYY') AS year_start,
TRUNC(DATE '2024-05-17', 'Q') AS quarter_start,
TRUNC(DATE '2024-05-17', 'IW') AS iso_week_start
FROM dual;For this example, day_start is May 17 at midnight, month_start is May 1, year_start is January 1, and quarter_start is April 1. The IW format model starts an ISO week on Monday.
Common format models include:
| Format model | Boundary returned |
|---|---|
| `DD` or omitted | Start of the day |
| `MM` | First day of the month |
| `Q` | First day of the quarter |
| `YYYY` | First day of the year |
| `IW` | Monday of the ISO week |
Choosing the right companion function
Use LAST_DAY when the business rule names a month's final date. Use NEXT_DAY for schedules such as “the next Friday.” Use TRUNC when you need midnight or the start of a month, quarter, year, or ISO week.
The DATE returned by these functions has second-level precision. If the requirement includes fractional seconds or explicit time-zone behavior, use TIMESTAMP and interval expressions instead.
Exercise
Write a query that returns the first and last day of the month containing DATE '2024-02-10', both at midnight.
Show answer
SELECT TRUNC(DATE '2024-02-10', 'MM') AS monthstart, TRUNC(LASTDAY(DATE '2024-02-10')) AS month_end FROM dual;