SQL 2000: semplice funzione per calcolare l'età in anni in base ad una certa data

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:

«ottobre»
domlunmarmergiovensab
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456