Home » SQL & PL/SQL » SQL & PL/SQL » Need to convert Number of Days in to Years Month Days format
|
|
|
|
|
|
| Re: Need to convert Number of Days in to Years Month Days format [message #138836 is a reply to message #138808] |
Sat, 24 September 2005 05:34   |
 |
rajavu1
Messages: 1574 Registered: May 2005 Location: Bangalore , India
|
Senior Member |

|
|
Hi,
Frank wrote :
| Quote: | How many days are there in a year? And in a month?
|
I don't think that,calculating days in between by taking care of such facts (a month is 30 days, a year is 365 days) will not be appropriate method as there can be 366 day in year,28/31 days a month.
Instead you can apply some day functions like ADD_MONTHS,MONTHS_BETWEEN as :
SQL> select YEARS,trunc(MB-YEARS*12) MONTHS,ND-add_months(OD,trunc(MB)) DAYS
2 from
3 (select OD,ND,months_between(ND,OD) MB,trunc(months_between(ND,OD)/12) YEARS from
4 (select sysdate nd,sysdate-365 od from dual));
YEARS MONTHS DAYS
---------- ---------- ----------
1 0 0
SQL> select YEARS,trunc(MB-YEARS*12) MONTHS,ND-add_months(OD,trunc(MB)) DAYS
2 from
3 (select OD,ND,months_between(ND,OD) MB,trunc(months_between(ND,OD)/12) YEARS from
4 (select sysdate nd,sysdate-366 od from dual));
YEARS MONTHS DAYS
---------- ---------- ----------
1 0 1
SQL> select YEARS,trunc(MB-YEARS*12) MONTHS,ND-add_months(OD,trunc(MB)) DAYS
2 from
3 (select OD,ND,months_between(ND,OD) MB,trunc(months_between(ND,OD)/12) YEARS from
4 (select sysdate nd,sysdate-395 od from dual));
YEARS MONTHS DAYS
---------- ---------- ----------
1 0 30
SQL> select YEARS,trunc(MB-YEARS*12) MONTHS,ND-add_months(OD,trunc(MB)) DAYS
2 from
3 (select OD,ND,months_between(ND,OD) MB,trunc(months_between(ND,OD)/12) YEARS from
4 (select sysdate nd,sysdate-396 od from dual));
YEARS MONTHS DAYS
---------- ---------- ----------
1 1 0
SQL> select YEARS,trunc(MB-YEARS*12) MONTHS,ND-add_months(OD,trunc(MB)) DAYS
2 from
3 (select OD,ND,months_between(ND,OD) MB,trunc(months_between(ND,OD)/12) YEARS from
4 (select sysdate nd,sysdate-397 od from dual));
YEARS MONTHS DAYS
---------- ---------- ----------
1 1 1
SQL>

Rajuvan.
[Updated on: Sat, 24 September 2005 06:15] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Thu Jun 25 01:59:33 CDT 2026
|