All DATEDIFF() is to subtract years from date components. It is so stupid:
select datediff(yy,'2000-12-19','2014-01-01') --14 select datediff(yy,'2000-12-19','2014-12-18') --14 select datediff(yy,'2000-12-19','2014-12-19') --14 select datediff(yy,'2000-12-19','2014-12-20') --14 select datediff(yy,'2000-12-19','2014-12-31') --14 select datediff(yy,'2000-12-19','2015-01-01') --15 select datediff(yy,'2000-12-19','2015-12-31') --15 select datediff(yy,'2000-12-19','2016-01-01') --16 select datediff(yy,'2000-12-19','2016-12-31') --16
Do not count the number of hours in a year with a year of 365.25 days or something like that. This exercise is useless and simply ensures that you will be wrong next to every birthday.
Itβs best to figure out how people do it. In the USA (and in most western countries, I think) this is the difference between the years, but you only count the current year when you celebrate your birthday:
declare @birthdate date = '2000-12-19'; declare @target date; SELECT DATEDIFF(yy, @birthdate, @target) - CASE WHEN (MONTH(@birthdate) > MONTH(@target)) OR ( MONTH(@birthdate) = MONTH(@target) AND DAY(@birthdate) > DAY(@target) ) THEN 1 ELSE 0 END
Here are the values ββyou received:
set @target = '2014-01-01' --13 set @target = '2014-12-18' --13 set @target = '2014-12-19' --14 set @target = '2014-12-20' --14 set @target = '2014-12-31' --14 set @target = '2015-01-01' --14 set @target = '2015-12-31' --15 set @target = '2016-01-01' --15 set @target = '2016-12-31' --16
Change @target to getdate() to calculate the current age.
If your region uses East Asian age , however, you will need to use a completely different method to determine how old a person is because they are considered age 1 on their birthday, and their age increases every February.