martedì 20 novembre 2012

T-SQL, Calculate Age From Date of Birth

Sometime could be useful calculate age from date of birth.
The following t-sql code shows how could be done it.

datediff(yy, date_of_birth, GETDATE()) -
       (case when (datepart(m date_of_birth) > datepart(m, GETDATE()))OR
                   (datepart(m date_of_birth) = datepart(m, GETDATE()) AND
                    datepart(d date_of_birth) > datepart(d, GETDATE()))
                        then 1
                        else 0
        end) AS AGE

My Two Cents...

Nessun commento:

Posta un commento