Loading ...

Calculate age from DateofBirth in sql | CodeAsp.Net

Calculate age from DateofBirth in sql

 /5
0 (0votes)

In this blog I will explain how to calculate age from DateofBirth in sql. Below is the code snippet.

DECLARE @DateofBirth DATETIME

DECLARE @CurrentDate DATETIME

SELECT @CurrentDate = CURRENT_TIMESTAMP

SELECT @DateofBirth = '19881024'

SELECT DATEDIFF(YEAR, @DateofBirth, @CurrentDate) -

(

CASE

WHEN

DATEPART(m,@DateofBirth) > DATEPART(m,@CurrentDate)

OR (DATEPART(m,@DateofBirth) = DATEPART(m,@CurrentDate) AND DATEPART(d,@DateofBirth) > DATEPART(d,@CurrentDate))

THEN 1 ELSE 0

END

) as [Age]

--Output

Age

-----------

20

 

 

Happy Reading.!

 

Comments (no comments yet)

Top Posts