SQL SERVER DATEDIFF函数 [英] SQL SERVER DATEDIFF FUNCTION

查看:70
本文介绍了SQL SERVER DATEDIFF函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WHY SELECT DATEDIFF(year,'2001-12-21 00:00:00.000' , '2015-12-17 00:00:00.000') in sql server is shows me 14 instead of 13

推荐答案

您可以找到关于datediff的非常好的讨论这里! [ ^ ]这应解决你的问题
You can find a very good discussion over datediff here![^] which should solve your problem


这是因为 DATEDIFF(YEAR,.. 不检查低于年份的单位,只减去年份,即2015-2001。

你可以检查

This is because DATEDIFF(YEAR,.. doesn't check units lower than year and just subtracts years i.e, 2015-2001 in your case.
You can check
SELECT DATEDIFF(year,'2001-12-21 00:00:00.000' , '2015-01-01 00:00:00.000')



,结果仍然会返回14.


and it will still return 14 as result.


读取了这个 DateDiff(Transact SQL) [ ^ ]



如果第一个日期改变年份则在那里说明即2001年至2002年,将计入年度总数。这就是为什么你得到14而不是13
have a read of this DateDiff (Transact SQL)[^]

it states in there if the first date then changes year i.e. from 2001 to 2002 that will be counted in the year total. that is why you get 14 instead of the 13


这篇关于SQL SERVER DATEDIFF函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆