Skip to Content
author's profile photo Former Member
Former Member

Convert No. of days to year and month.

Hi Experts,

I have a scenario where i need to calculate No. of years and No. of months between 2 dates.

I have calculated no of days between 2 dates using this conversion.

interval_to_char(sysdate()-PURCHASE_DT,'D')

For eg. Suppose no of days is 765 then in YEAR column 2 should be mapped and in MONTH column 1 should be mapped.

No. of Days Year Month

765 2 1

For Year i have used this:

floor(cast(interval_to_char(sysdate()-PURCHASE_DT,'D'),'INT')/365)

Please help me with Month Column.

Thanks

Neha Khetan

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Posted on Mar 14, 2014 at 06:16 AM

    Hi Neha,

    try this,

    for no of years ---- date_diff('2011.09.12','2013.06.26','YY')

    for no of months -----date_diff('2011.09.12','2013.06.26','MM')%12

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 14, 2014 at 06:27 AM

    Hi Neha,


    I would recommend you to use datatype as decimal(10,2) instead of INT to calculate the exact Years and months for given no. of days.

    For YEAR you can use below code:

    (cast(interval_to_char(sysdate()-PURCHASE_DT,'D'),'DECIMAL(10,2)')/365)

    For MONTH you can use below code:


    (cast(interval_to_char(sysdate()-PURCHASE_DT,'D'),'DECIMAL(10,2)')/365)*12


    If you take a date 29-04-2009 as an example then above code shall give the following result.


    DAYS: 1780
    YEAR: 4.87
    MONTH: 58.52

    Hope this is what you are looking for.

    Regards !

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.