SQL Server - DATEDIFF 舍入错误
在我的 sql 语句中,我需要检索 datediff
超过 3 个月的行。
但我发现它似乎有舍入问题,例如
起始日期:2010-09-09
截止日期:2010-12-01
select datediff(month,' 2010-09-09', '2010-12-01')
结果返回 3。
如何修复它?谢谢。
问候, 乔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用
You could use
Datediff(month, date1, date2) 只会比较月份部分之间的差异,不考虑天数。
为了计算日期之间的实际月份数,您必须做一些手动工作。
这个答案
看起来与您要问什么,但您可能需要根据您对构成“月”差异的具体定义(包括部分月份?)进行修改。
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?).