calculate age

/*
select dbo.calc_age(‘1971-03-06’, ‘2016-02-04’ )
select dbo.calc_age(‘1971-03-06’, ‘2016-03-04’ )
select dbo.calc_age(convert(date, ‘1977-03-06’), convert(date, ‘2016-03-05’) )
select dbo.calc_age(‘1971-03-06’, ‘2016-03-06’ )
select dbo.calc_age(‘1971-03-06’, ‘2016-03-08’ )
select dbo.calc_age(‘1971-03-06’, ‘2016-11-01’ )

*/

alter function dbo.calc_age ( @birth_date date, @t1 date)
returns int as
begin
declare @age as int

select @age = DATEDIFF(yy, @birth_date, @t1) –
CASE — before birthday
WHEN month(@t1)<month(@birth_date)
or (month(@t1)=month(@birth_date) and
day(@t1) < day(@birth_date) )
THEN 1
ELSE 0
END
return @age
end

Leave a Reply

Your email address will not be published. Required fields are marked *