Wednesday, 1 April 2009

Calculate Age using Sql server

This simple SQL will help you to calcuate you age using SQL server


DECLARE @BirthDate DATETIME
DECLARE @CurrentDate DATETIME

SELECT @CurrentDate = getdate(), @BirthDate = '11/24/1984' --Add your date of birth here

SELECT DATEDIFF(YY, @BirthDate, @CurrentDate) -
CASE
WHEN( (MONTH(@BirthDate)*100 + DAY(@BirthDate)) > (MONTH(@CurrentDate)*100 + DAY(@CurrentDate)) )
THEN 1 ELSE 0 END


Please leave your comments if it helped you!!

3 comments:

  1. ya, it really help me :) thanks

    ReplyDelete
  2. Thanks... the scripted worked great for me....:)

    ReplyDelete
  3. It worked great. Thanks for keeping it simple and easy to understand.

    Swapna

    ReplyDelete