Sql Server: Calculate Age in years from Date of Birth (DOB) - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Sql Server: Calculate Age in years from Date of Birth (DOB)

In this article I am going to explain how to calculate the age in years from given Date of Birth in Sql.


Implementation:

You can calculate the age from DOB using below given methods:

Method 1:
Declare @DOB DATE ='09/28/1980'
Declare @Currentdate date = getdate()
SELECT [Date Of Birth] = @DOB,[Current Date]=@Currentdate,[Age]=DATEDIFF(yy,@DOB,@Currentdate)


Method 2:
SELECT [Age in Years] = DATEDIFF(DAY, '09/28/1980', GetDate()) / 366


Method 3:
Declare @DOB DATE ='09/28/1980'
Declare @Currentdate date = getdate()
SELECT[Date of Birth] = @DOB,[Current Date]=@Currentdate,[Age]=DATEDIFF(yy,@DOB,@Currentdate)-
   (CASE WHEN DATEADD(YY,DATEDIFF(YEAR,@DOB,@Currentdate),@DOB)>  @Currentdate THEN 1 ELSE 0 END)


Method 4:
Declare @DOB DATE ='07/28/1980'
Declare @Currentdate date = getdate()
SELECT [Date Of Birth] = @DOB,[Current Date]=@Currentdate,[Age]=DATEDIFF(dd,@DOB,@Currentdate)/365.25




What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon