Calculating the Number of Days in a Year in Oracle
In Oracle, the number of days in a year can vary depending on whether it's a leap year (366 days) or a non-leap year (365 days). Below are different ways to determine this:
✅ 1️⃣ Basic Calculation for Any Given Year
🔹 This calculates the difference between December 31 and January 1, adding +1
to include both days.
🟢 Output
✅ 2️⃣ Using EXTRACT(YEAR FROM SYSDATE)
Dynamically
🔹 This dynamically calculates the days in the current year.
✅ 3️⃣ Checking If a Year is a Leap Year
You can check whether a specific year has 366 days (leap year) or 365 days (normal year).
🔹 If Feb 29 exists, it's a leap year (366 days); otherwise, it's 365 days.
🟢 Example Output
✅ 4️⃣ Using ADD_MONTHS
Function
🔹 This adds 12 months to January 1st and subtracts it to get the number of days.
🔹 Summary
Method | Query | Output Example |
---|---|---|
Basic Static Query | TO_DATE('31-DEC-YYYY') - TO_DATE('01-JAN-YYYY') + 1 | 365 or 366 |
Dynamic Current Year | EXTRACT(YEAR FROM SYSDATE) | 365 or 366 |
Leap Year Check | LAST_DAY(TO_DATE('29-FEB-YYYY')) = 29 | 365 / 366 |
Using ADD_MONTHS | ADD_MONTHS(TO_DATE('01-JAN-YYYY'), 12) - TO_DATE('01-JAN-YYYY') | 365 / 366 |
Tags:
Oracle