Calculating Age of the person in T-SQL


One of common calculations, we have to do in ETL is calculating age of the person from the date attribute

And this is how I have calculated Age of the person.


Select BirthDate,
CASE
            WHEN Month(GetDate()) < Month(c.[BirthDate])
                THEN DateDiff(yy,c.[BirthDate],GetDate()) - 1
            WHEN Month(GetDate()) = Month(c.[BirthDate])
            AND Day(GetDate()) < Day(c.[BirthDate])
                THEN DateDiff(yy,c.[BirthDate],GetDate()) - 1
            ELSE DateDiff(yy,c.[BirthDate],GetDate())
        END AS [Age]

              from [dbo].[DimCustomer] c
              order by BirthDate



I am sure, there would be other better ways to do this and would love to hear, of how you have calculated age of the person


Comments

Popular posts from this blog

How to Troubleshoot Subscription issue in Reporting Services