2

In my sql statement, I need to retrieve rows which datediff more than 3 months.

But I found that it seems have rounding problem such as

From Date: 2010-09-09
To Date: 2010-12-01

select datediff(month,' 2010-09-09', '2010-12-01')

It returns 3 for result.

How to fix it ? thanks.

regards, Joe

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Joe Yan
  • 2,025
  • 8
  • 43
  • 62

2 Answers2

2

You could use

datediff(day,@d1,@d2) >= 90
Matt
  • 3,638
  • 2
  • 26
  • 33
1

Datediff(month, date1, date2) will only compare the difference between the month parts and does not take days into account.

In order to calculate the real number of months between the dates, you will have to do some manual work.

This answer

looks to be a match to what you are asking but you may have to modify depending on your specific definition of what constitutes a 'month' difference (partial months included?).

Community
  • 1
  • 1
nycdan
  • 2,819
  • 2
  • 21
  • 33