Sub topic 10
Calculating Age and Tenure
Assume you know basic SELECT statements, WHERE, the DUAL table, and numeric expressions. This chapter uses Oracle DATE values and MONTHS_BETWEEN to calculate completed years, remaining months, and elapsed tenure.
The basic age calculation
Age is usually the number of completed years between a birth date and an as-of date. Tenure uses the same calculation with an employee's start date.
MONTHS_BETWEEN returns the number of months between two dates. Dividing by 12 converts months to years. TRUNC removes the incomplete year.
SELECT TRUNC(
MONTHS_BETWEEN(DATE '2026-11-28', DATE '1990-06-15') / 12
) AS completed_years
FROM dual;This query returns the completed age as of November 28, 2026. Use an explicit as-of date when reproducibility matters. Use SYSDATE when the calculation should use the database server's current date.
SELECT TRUNC(MONTHS_BETWEEN(SYSDATE, birth_date) / 12) AS age_years
FROM people;For a future birth date or start date, the result can be negative. A NULL input produces NULL, so check the data rules if every row must have a result.
Calculating years and extra months
A report may need both completed years and the months after those years. First calculate whole months. Then divide by 12 for years and use MOD for the remainder.
SELECT TRUNC(MONTHS_BETWEEN(SYSDATE, DATE '1990-06-15') / 12) AS age_years,
MOD(TRUNC(MONTHS_BETWEEN(SYSDATE, DATE '1990-06-15')), 12)
AS extra_months
FROM dual;For tenure, replace the birth date with an employment start date:
SELECT employee_id,
TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date) / 12) AS tenure_years,
MOD(TRUNC(MONTHS_BETWEEN(SYSDATE, hire_date)), 12)
AS extra_months
FROM employees;This expresses tenure as “years and remaining months.” It does not claim that every month has the same number of days.
Why fractional months need care
Oracle calculates many unequal-day month differences using a 31-day month. The time portion can also affect the result. When the dates have the same day of the month, or both are month ends, the result is commonly an integer. Otherwise, the result may contain a fraction.
SELECT MONTHS_BETWEEN(DATE '2024-02-10', DATE '2024-01-01')
AS fractional_months
FROM dual;Do not round this value before calculating completed years. ROUND could turn an incomplete year into a completed year. TRUNC is the safer choice for “completed” units.
A reliable calculation sequence
For a strict birthday-style age, calculate a preliminary year count and then check whether the anniversary has occurred:
SELECT birth_date,
TRUNC(MONTHS_BETWEEN(SYSDATE, birth_date) / 12)
AS preliminary_age
FROM people;This approach is concise, but it inherits MONTHS_BETWEEN rules. If the exact anniversary is important, compare ADD_MONTHS(birth_date, years * 12) with the as-of date and reduce the year count when that anniversary is still in the future.
Exercise
Why should completed age and tenure calculations use TRUNC instead of ROUND on the result of MONTHS_BETWEEN divided by 12?
Show answer
TRUNC counts only fully completed years. ROUND could increase an incomplete year to the next whole year.