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

后端 未结 10 1974
萌比男神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条回答
  • SELECT   (MONTHS_BETWEEN(date2,date1) +  (datediff(day,date2,date1))/30) as num_months,
    datediff(day,date2,date1) as diff_in_days  FROM  dual;
    
    // You should replace date2 with TO_DATE('2012/03/25', 'YYYY/MM/DD')
    // You should replace date1 with TO_DATE('2012/01/01', 'YYYY/MM/DD')
    // To get you results
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • See the query below (assumed @dt1 >= @dt2);

    Declare @dt1 datetime = '2013-7-3'
    Declare @dt2 datetime = '2013-5-2'
    
    select abs(DATEDIFF(DD, @dt2, @dt1)) Days,
    case when @dt1 >= @dt2
        then case when DAY(@dt2)<=DAY(@dt1)
            then Convert(varchar, DATEDIFF(MONTH, @dt2, @dt1)) + CONVERT(varchar, ' Month(s) ') + Convert(varchar, DAY(@dt1)-DAY(@dt2)) + CONVERT(varchar, 'Day(s).')
            else Convert(varchar, DATEDIFF(MONTH, @dt2, @dt1)-1) + CONVERT(varchar, ' Month(s) ') + convert(varchar, abs(DATEDIFF(DD, @dt1, DateAdd(Month, -1, @dt1))) - (DAY(@dt2)-DAY(@dt1))) + CONVERT(varchar, 'Day(s).')
        end
        else 'See asumption: @dt1 must be >= @dt2'
    end In_Months_Days
    

    Returns:

    Days | In_Months_Days
    
    62   |   2 Month(s) 1Day(s).
    
    0 讨论(0)
  • 2020-12-31 08:08

    I think that your question is not defined well enough, for the following reason.

    Answers relying on months_between have to deal with the following issue: that the function reports exactly one month between 2013-02-28 and 2013-03-31, and between 2013-01-28 and 2013-02-28, and between 2013-01-31 and 2013-02-28 (I suspect that some answerers have not used these functions in practice, or are now going to have to review some production code!)

    This is documented behaviour, in which dates that are both the last in their respective months or which fall on the same day of the month are judged to be an integer number of months apart.

    So, you get the same result of "1" when comparing 2013-02-28 with 2013-01-28 or with 2013-01-31, but comparing it with 2013-01-29 or 2013-01-30 gives 0.967741935484 and 0.935483870968 respectively -- so as one date approaches the other the difference reported by this function can increase.

    If this is not an acceptable situation then you'll have to write a more complex function, or just rely on a calculation that assumes 30 (for example) days per month. In the latter case, how will you deal with 2013-02-28 and 2013-03-31?

    0 讨论(0)
提交回复
热议问题