• ColdCoffee (5/26/2010)


    You can use DATEDIFF function:

    LIke:

    SET DATEFORMAT DMY

    SELECT DATEDIFF(yy,'21-03-1985',GETDATE()) AGE

    Since DateDiff returns the number of boundaries crossed in comparing these two dates, it will report the wrong age if today's date is not >= your DOB. So, use this instead:

    SELECT CASE WHEN datepart(dayofyear, GetDate() ) >= datepart(dayofyear, @DOB)

    THEN DATEDIFF(yy,@DOB,GETDATE())

    ELSE DATEDIFF(yy,@DOB,GETDATE()) -1

    END AGE

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2