Skip to content
Advertisement

Oracle – get previous, current and next year from query [closed]

Is below example good solution for get previous, current and next year?

Or any better solution?

Advertisement

Answer

Well, yours isn’t correct for e.g. leap years on the last day of that year. Look at 2020:

See? Turns out that “previous” year for date 31.12.2020 is still 2020.

ADD_MONTHS is safer, I guess:


(this, of course, doesn’t require add_months, but I kept it to make query look prettier).


Or, why not simply

For 3 rows, use a CTE:

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement