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

后端 未结 10 1993
萌比男神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 08:02

    The solution I post will consider a month with 30 days

      select CONCAT (CONCAT (num_months,' MONTHS '), CONCAT ((days-(num_months)*30),' DAYS '))
      from ( 
      SELECT floor(Months_between(To_date('20120325', 'YYYYMMDD'),
       To_date('20120101', 'YYYYMMDD')))
       num_months,
       ( To_date('20120325', 'YYYYMMDD') - To_date('20120101', 'YYYYMMDD') )
       days
      FROM   dual);
    

提交回复
热议问题