Get the difference between two dates both In Months and days in sql

后端 未结 10 1975
萌比男神i
萌比男神i 2020-12-31 07:43

I need to get the difference between two dates say if the difference is 84 days, I should probably have output as 2 months and 14 days, the code I have just gives the totals

10条回答
  •  时光取名叫无心
    2020-12-31 07:53

    is this what you've ment ?

    select trunc(months_between(To_date('20120325', 'YYYYMMDD'),to_date('20120101','YYYYMMDD'))) months,
                 round(To_date('20120325', 'YYYYMMDD')-add_months(to_date('20120101','YYYYMMDD'),
                               trunc(months_between(To_date('20120325', 'YYYYMMDD'),to_date('20120101','YYYYMMDD'))))) days
            from dual;
    

提交回复
热议问题