开发者

SQL Server - DATEDIFF rounding error

开发者 https://www.devze.com 2023-04-09 05:39 出处:网络
In my s开发者_运维问答ql statement, I need to retrieve rows which datediff more than 3 months.

In my s开发者_运维问答ql 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


You could use

datediff(day,@d1,@d2) >= 90


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?).

0

精彩评论

暂无评论...
验证码 换一张
取 消