create function dbo.AgeLastBirthday(@dob date) returns int as begin declare @today date = getdate() declare @Years int = year(@today) - year(@dob) declare @LastBirthday date = dateadd(year, @Years, @dob) if @LastBirthday > @today begin set @Years = @Years - 1 --set @LastBirthday = dateadd(year, @Years, @dob) end return @Years end