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
posted @ venerdì 22 ottobre 2004 04:54