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

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

    MsSql Syntax : DATEDIFF ( datepart , startdate , enddate )

    Oracle: This will returns number of days

        select
      round(Second_date - First_date)  as Diff_InDays,round ((Second_date - First_date) / (30),1)  as Diff_InMonths,round ((Second_date - First_date) * (60*24),2)  as TimeIn_Minitues
    from
      (
      select
        to_date('01/01/2012 01:30:00 PM','mm/dd/yyyy hh:mi:ss am') as First_date
       ,to_date('05/02/2012 01:35:00 PM','mm/dd/yyyy HH:MI:SS AM') as Second_date
      from
        dual
      ) result;
    

    Demo : http://sqlfiddle.com/#!4/c26e8/36

提交回复
热议问题