hariharannkl (9/7/2013)
It is very simple, just use the following sql statement,(DATEDIFF(YY,DOB,GETDATE()) - CASE
WHEN MONTH(DOB)<MONTH(GETDATE()) AND (MONTH(DOB)=MONTH(GETDATE()) OR DAY(DOB)>DAY(GETDATE())) THEN 0 ELSE 1
END)
Here DOB is Columname, and GETDATE() is a function which gives the current date.
It would appear that your's also has a Leap Year problem but in the opposite direction. Your's returns "0" for the following dates which is incorrect.
DECLARE @DOB DATETIME
,@Now DATETIME
SELECT @DOB = '2000-03-31'
,@Now = '2001-03-31'
SELECT DATEDIFF(YY,@DOB,@Now) - CASE
WHEN MONTH(@DOB)<MONTH(@Now) AND (MONTH(@DOB)=MONTH(@Now) OR DAY(@DOB)>DAY(@Now)) THEN 0 ELSE 1
END
--Jeff Moden
Change is inevitable... Change for the better is not.