How to calculate somebody's age from their Date of Birth in SQL?

All topics about programming / development in T-SQL or other languages for SQL Server.
Post Reply
Allan
Posts: 7
Joined: Fri Jul 05, 2019 3:42 pm

Mon Jul 08, 2019 6:31 pm

Hi, how can I calculate somebody's age from their Date of Birth in SQL?

I have the below variable which i would like to convert to somebodys DOB:

Code: Select all

DECLARE @DateOfBirth  datetime
SET @DateOfBirth ='1995-11-22 00:00:00'
DaveP5
Posts: 29
Joined: Sat Jun 22, 2019 7:58 pm
Answers: 1

Mon Jul 08, 2019 6:35 pm

Hi @Allan

This can be easily achieved with the below

Code: Select all

DECLARE @DateOfBirth  datetime
SET @DateOfBirth='1995-11-22 00:00:00'

SELECT DATEDIFF(hour,@DateOfBirth,GETDATE())/8766.0 AS AgeYearsDecimal
    ,CONVERT(int,ROUND(DATEDIFF(hour,@DateOfBirth,GETDATE())/8766.0,0)) AS AgeYearsIntRound
    ,DATEDIFF(hour,@DateOfBirth,GETDATE())/8766 AS AgeYearsIntTrunc
   
Post Reply

Social Media