Don't use DATEDIFF
. When using DATEDIFF(year, ...)
it is only the year part of the dates that is being considered. So it wouldn't matter which birthday exactly and you'd consider people 20 years old who are not yet full twenty years old.
Today 2015-12-26
you would want persons born between 1984-12-17
till 1995-12-16
.
1984-12-17
(today minus 31 years plus one day) means the person gets 31 tomorrow
1995-12-16
(today minus 20 years) means the person just got 20 today.
So:
select *
from persons
where convert(varchar, dob, 102)
between convert(varchar, dateadd(day, 1, dateadd(year, -31, getdate())), 102)
and convert(varchar, dateadd(year, -20, getdate()), 102);
(I must admit though that I am too lazy to think the February 29 thing through now.)