Sub topic 10
Calculating age and tenure
Age and tenure both answer the same question: how many full years, months, and days lie between a start date and an as-of date? This chapter assumes you already know SYSDATE, TRUNC, ADD_MONTHS, and MONTHS_BETWEEN from earlier chapters, including the end-of-month rule and the 31-day rule for fractional months. Here you combine them into queries that give correct answers on birthdays, month ends, and leap days.
Why dividing days by 365 fails
The obvious approach is to subtract two dates and divide by 365. Years are not all 365 days long, so the error accumulates. Each leap year adds a day, and over decades those days push the result past the real birthday.
SELECT TRUNC((DATE '2019-12-31' - DATE '1950-01-01') / 365) AS days_method,
TRUNC(MONTHS_BETWEEN(DATE '2019-12-31', DATE '1950-01-01') / 12) AS months_method
FROM dual;The date range is one day short of a 70th birthday, so the correct age is 69. The range spans 25,566 days, and 25,566 / 365 is about 70.04, so the days method returns 70. MONTHS_BETWEEN counts calendar months, so it returns 839 months, and 839 / 12 truncates to 69.
Dividing by 365.25 reduces the drift but still fails near birthdays in some years. Use calendar months instead.
The core formula
Age in completed years is the number of completed months divided by 12, truncated. The extra months are the remainder.
SELECT TRUNC(MONTHS_BETWEEN(TRUNC(SYSDATE), DATE '1990-06-15') / 12) AS age_years,
MOD(TRUNC(MONTHS_BETWEEN(TRUNC(SYSDATE), DATE '1990-06-15')), 12) AS extra_months
FROM dual;The inner TRUNC on MONTHS_BETWEEN matters. It discards the fractional part, which represents an incomplete month. Without it, MOD would return a fraction such as 4.52.
I wrapped SYSDATE in TRUNC so that the as-of value is midnight. MONTHS_BETWEEN returns an exact integer when both dates fall on the same day of the month, whatever the time of day. On other days it includes the time of day in the fraction. After truncation, the time of day could change the answer only in rare edge cases. Using midnight on both sides removes that doubt, and it also makes the result easy to reproduce.
Why truncation, not rounding
A result of 407.97 months means 33 years and 11 months, not 34 years. The person has not completed the 408th month yet. ROUND would give 34 and report a birthday a day early. Always use TRUNC or FLOOR for completed units. Use ROUND only for a decimal tenure figure for display, such as 2.5 years, where an approximation is acceptable.
Years, months, and days together
A full breakdown needs the leftover days. Find the date reached by adding the completed months to the start date, then subtract it from the as-of date.
WITH people AS (
SELECT 'Ana' AS person_name, DATE '1990-06-15' AS start_date, DATE '2024-06-14' AS as_of FROM dual
UNION ALL
SELECT 'Ben', DATE '1990-06-15', DATE '2024-06-15' FROM dual
UNION ALL
SELECT 'Cy', DATE '2000-02-29', DATE '2025-02-28' FROM dual
)
SELECT person_name,
TRUNC(MONTHS_BETWEEN(as_of, start_date) / 12) AS whole_years,
MOD(TRUNC(MONTHS_BETWEEN(as_of, start_date)), 12) AS whole_months,
as_of - ADD_MONTHS(start_date,
TRUNC(MONTHS_BETWEEN(as_of, start_date))) AS leftover_days
FROM people;| PERSON_NAME | WHOLE_YEARS | WHOLE_MONTHS | LEFTOVER_DAYS |
|---|---|---|---|
| Ana | 33 | 11 | 30 |
| Ben | 34 | 0 | 0 |
| Cy | 25 | 0 | 0 |
Ana's as-of date is one day before her 34th birthday. Her completed months are 407, or 33 years and 11 months. ADD_MONTHS(DATE '1990-06-15', 407) is 2024-05-15, and 30 days separate that date from 2024-06-14.
The same ADD_MONTHS and MONTHS_BETWEEN pair is consistent with itself. Both apply the end-of-month rule, so the leftover days never come out negative for a valid range.
Birthdays on month ends and leap days
Because both functions use the end-of-month rule, a few cases behave in ways that surprise people.
| Start date | As-of date | Result | Reason |
|---|---|---|---|
| 2000-02-29 | 2025-02-28 | 25 years | Both are month ends, so the result is exactly 300 months. |
| 2000-02-29 | 2025-02-27 | 24 years | Not both month ends. The fraction is 300 + (27 - 29) / 31, about 299.94. |
| 2000-02-29 | 2025-03-01 | 25 years | 300 months plus one day. |
| 2020-01-31 | 2020-02-29 | 1 month | Both are month ends. |
For a person born on 29 February, Oracle treats 28 February as the birthday in non-leap years. Some legal systems use 1 March instead. If your business rule differs, state the rule in a comment and test it. Do not assume the database matches it.
The Jan 31 case has a similar effect. An employee hired on 2020-01-31 completes one month on 2020-02-29, and on 2020-03-31 completes two months, not on 2020-03-29.
Edge cases to guard against
A guarded version looks like this:
SELECT CASE
WHEN birth_date IS NULL THEN NULL
WHEN birth_date > SYSDATE THEN NULL
ELSE TRUNC(MONTHS_BETWEEN(TRUNC(SYSDATE), TRUNC(birth_date)) / 12)
END AS age_years
FROM employees;I applied TRUNC to birth_date as well, in case the column holds times from an import. Without it, the fractional part includes the stored time, which can shift the result by a tiny amount on days that do not share a day of the month.
Tenure as a decimal
Reports often want a number like 4.7 years. Divide a fractional month count by 12 and round only at the end.
SELECT employee_id,
ROUND(MONTHS_BETWEEN(TRUNC(SYSDATE), hire_date) / 12, 1) AS tenure_years
FROM employees;This figure is a display value. Do not use it for eligibility rules such as "at least 5 years of service," because 4.96 rounds to 5.0. Use a date comparison for those rules, as described next.
Filtering by age or tenure
Applying a function to a column in WHERE prevents a normal index on that column from being used.
-- Works, but wraps the column in a function.
SELECT employee_id FROM employees
WHERE MONTHS_BETWEEN(TRUNC(SYSDATE), hire_date) >= 60;Move the arithmetic to the other side so the column stays bare.
-- At least 5 years of service: hired on or before the date 60 months ago.
SELECT employee_id, hire_date
FROM employees
WHERE hire_date < ADD_MONTHS(TRUNC(SYSDATE), -60) + 1;The + 1 and the < operator include every row stamped any time on the boundary day. Using <= against midnight would drop rows stored with a time such as 14:30 on that date. The result also matches the MONTHS_BETWEEN rule, because ADD_MONTHS applies the same end-of-month logic. An adult filter at 18 years works the same way:
SELECT person_id
FROM people
WHERE birth_date < ADD_MONTHS(TRUNC(SYSDATE), -216) + 1;Choosing a method
| Need | Approach |
|---|---|
| Completed years for display | `TRUNC(MONTHS_BETWEEN(...) / 12)` |
| Years, months, days | Completed months, then `ADD_MONTHS` to find leftover days |
| Decimal tenure for a report | `ROUND(MONTHS_BETWEEN(...) / 12, 1)` |
| Eligibility filter on an indexed column | Compare the column to `ADD_MONTHS(TRUNC(SYSDATE), -n) + 1` |
| Legal rule for leap-day births | Write the rule down and test it against 28 Feb and 1 Mar |
Summary
- Subtracting days and dividing by 365 drifts because of leap years. Count calendar months instead.
- Truncate
MONTHS_BETWEEN, do not round it, when you want completed units. - Compute leftover days by subtracting
ADD_MONTHS(start, total_months)from the as-of date. - Month-end and leap-day cases follow Oracle's end-of-month rule. Confirm that it matches your business rule.
- For filters, keep the column bare and compare it with an
ADD_MONTHSboundary plus one day.
Exercise
An employee was hired on 2021-08-31. As of 2024-02-29, how many whole years, months, and leftover days of tenure does the formula give? Show how you get each number.
Show answer
Both dates are the last day of their month, so MONTHSBETWEEN returns an integer: (2024 - 2021) * 12 + (2 - 8) = 30 months. Years = TRUNC(30 / 12) = 2, months = MOD(30, 12) = 6. ADDMONTHS(DATE '2021-08-31', 30) is 2024-02-29, because the input is a month end and the result is the last day of the target month. Leftover days = 2024-02-29 - 2024-02-29 = 0. The answer is 2 years, 6 months, 0 days.
Exercise
Write a WHERE clause that selects customers whose birthdate column may contain times of day and who are at least 21 years old today, without wrapping birthdate in a function.
Show answer
WHERE birthdate < ADDMONTHS(TRUNC(SYSDATE), -252) + 1. The value ADDMONTHS(TRUNC(SYSDATE), -252) is the date exactly 21 years ago at midnight. Adding 1 and using < includes every birthdate stamped any time on that day, and the column stays unmodified so an index can be used.