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

后端 未结 10 1935
萌比男神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:46

    Find out Year - Month- Day between two Days in Orale Sql


    select 
    trunc(trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD')))/12) years ,
    trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD'))) 
    -
    (trunc(trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD')))/12))*12
    months,
                 round(To_date('20120101', 'YYYYMMDD')-add_months(to_date('19910228','YYYYMMDD'),
                               trunc(months_between(To_date('20120101', 'YYYYMMDD'),to_date('19910228','YYYYMMDD'))))) days
            from dual;
    

提交回复
热议问题