Solved

# computed age column

Posted on 2014-04-17
430 Views
I want a computed age column based on DOB.  What the best way to do this?  Is there a built in age function?
0
Question by:HLRosenberger
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 33

Accepted Solution

Big Monty earned 250 total points
ID: 40007048
no, but you can do something like this:

``````DECLARE @dob  datetime
SET @dob='1992-01-09 00:00:00'

SELECT DATEDIFF(hour,@dob,GETDATE())/8766.0 AS AgeYearsDecimal
,CONVERT(int,ROUND(DATEDIFF(hour,@dob,GETDATE())/8766.0,0)) AS AgeYearsIntRound
,DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc
``````
0

LVL 69

Assisted Solution

Scott Pletcher earned 250 total points
ID: 40007135
>> What the best way to do this? <<

Here's my preferred method.

SELECT
DATEDIFF(YEAR, dob, GETDATE()) - CASE WHEN CONVERT(char(5), dob, 1) > CONVERT(char(5), GETDATE(), 1) THEN 1 ELSE 0 END as age

Leap year birthdays are the tricky part.  The code above treats Mar 1 as the birthday for someone born on Feb 29.  Code will need adjusted if you need to consider Feb 28 their birthday in non-leap years (believe it or not, some govts actually have laws on how this needs calculated, esp. for retirement, years-of-service, etc.).

>>  Is there a built-in age function? <<
No.
0

LVL 1

Author Closing Comment

ID: 40009258
thanks!
0

LVL 69

Expert Comment

ID: 40009345
Interesting choice.  Exactly one year (or nn years) after my birthday, shouldn't I be one year (or nn years) old?!

DECLARE @dob  datetime

SELECT @dob AS date_of_birth, GETDATE() AS todays_date,
DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc

SELECT @dob AS date_of_birth, GETDATE() AS todays_date,
DATEDIFF(hour,@dob,GETDATE())/8766 AS AgeYearsIntTrunc
0

LVL 1

Author Comment

ID: 40009435
ScottPletcher

I do not follow.  I'm working with both solutions trying to get he correct age.
0

LVL 69

Expert Comment

ID: 40009584
OK, good luck.  I can't imagine that an hours-based method could ever be fully accurate.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differeâ€¦
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that witâ€¦
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
###### Suggested Courses
Course of the Month7 days, 5 hours left to enroll