In Oracle, I need to calculate a 1-based value of the number of years that have elapsed since a given date, accounting for leap years.
The original calculation did not account for leap years. It was
CEIL ((SYSDATE- prog_start_dt) / 365))
Then I decided to use MONTHS_BETWEEN, but there is a bug in my new calculation:
CEIL(MONTHS_BETWEEN(SYSDATE, prog_start_dt) / 12)
The bug is that if I'm exactly 1 year after prog_start_date regardless of time, the value is 1 less. Ex.:
sysdate = 2020-07-01 15:30:00
prog_start_dt = 2019-07-01 00:00:00
--> 1. CEIL ((SYSDATE- prog_start_dt) / 365)) --> Result: 2
--> 2. CEIL(MONTHS_BETWEEN(SYSDATE, prog_start_dt) / 12) --> Result 1
The correct result is 2. So I need the behavior of the top formula, but accounting for leap years, which obviously won't be 365. Is there a good solution?