CREATE FUNCTION [dbo].[fn_GetAgeInYearsOnBDay] (@birthday datetime, @d datetime)
RETURNS int AS
BEGIN
declare @age int
select @age = datediff(yy, @birthday, @d) -
(case when (datepart(m, @birthday) > datepart(m, @d))OR
(datepart(m, @birthday) = datepart(m, @d) AND
datepart(d, @birthday) > datepart(d, @d))
then 1
else 0
end)
Return(@age)
END
USAGE: select dbo.fn_GetAgeInYearsOnBDay('07/16/1971', Getdate())
Technorati tags: SQL