Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Posted

You can find a very good discussion over datediff here![^] which should solve your problem
 
Share this answer
 
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
SQL
SELECT DATEDIFF(year,'2001-12-21 00:00:00.000' , '2015-01-01 00:00:00.000')

and it will still return 14 as result.
 
Share this answer
 
v2
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
 
Share this answer
 
Comments
Member 11463653 8-Dec-15 7:24am    
than why did they are taking date as a argument instead of year(int), if they are taking date as argument than they should calculate on date not just part of date.
please make me correct if I am wrong.
Simon_Whale 8-Dec-15 11:30am    
becuase then you should of used YEAR(datecolumn) - YEAR(datecolumn)

https://msdn.microsoft.com/en-GB/library/ms186313.aspx?f=255&MSPPError=-2147217396

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900