Solved

T-SQL: calculating person age

Posted on 2013-11-01
3
1,478 Views
Last Modified: 2013-11-19
One table column has Date of Birth.
I have to calculate person's age in  years, for example 1.23 ,  8.35 , 37.36 etc. in another column.
Any thoughts on this?
0
Comment
Question by:quasar_ee
[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
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
Simone B earned 250 total points
ID: 39617946
DATEDIFF returns an integer, so you have to cast or convert to a decimal. If you use 365.25 days per year instead of 365, that will cover for the leap years. Then to round the whole thing to 2 decimal places, cast again:

SELECT CAST(CAST(DATEDIFF(d,YourDateField,GETDATE()) AS DECIMAL(10,2))/365.00 AS DECIMAL(10,2))
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 39619158
Buttercup1,

I like it, but I see a couple of problems, such as:
In either formula, having two decimal places is not enough precision, and
In the 365.25 formula, you have a risk that you will understate the true age in some edge cases

Illustrating the first, consider this example:

DECLARE @dob datetime = '2012-02-28',
    @today datetime = '2013-02-27'
    
SELECT CAST(CAST(DATEDIFF(d, @dob, @today) AS DECIMAL(10, 2)) / 365.00 AS DECIMAL(10, 2)) AS _365,
    CAST(CAST(DATEDIFF(d, @dob, @today) AS DECIMAL(10, 2)) / 365.25 AS DECIMAL(10, 2)) AS _36525

Open in new window


This returns 1.00 and 1.00, respectively, yet most people would agree that this person is not yet a year old.  Using only two digits for the decimal portion is not precise enough.  Using decimal(10, 4) instead would remedy that.

Now consider how using 365.25 can understate the age in some edge cases.  (I am adopting the more precise decimal(10, 4) here.)

DECLARE @dob datetime = '2013-02-28',
    @today datetime = '2014-02-28'
    
SELECT CAST(CAST(DATEDIFF(d, @dob, @today) AS DECIMAL(10, 4)) / 365.00 AS DECIMAL(10, 4)) AS _365,
    CAST(CAST(DATEDIFF(d, @dob, @today) AS DECIMAL(10, 4)) / 365.25 AS DECIMAL(10, 4)) AS _36525

Open in new window


The "365" formula returns 1.0000, and the "365.25" formula returns 0.9993, but everyone would agree that such a person should be counted as one year old.

:)

Patrick
0
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 250 total points
ID: 39619233
Inelegant, and there may well be a better way, but this does address the points I made above:

SELECT 
    DOB, GETDATE(),
    DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB),
    DATEADD(year, DATEDIFF(year, DOB, GETDATE()) - 1, DOB),
    CASE 
    WHEN CONVERT(date, DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB)) > CONVERT(date, GETDATE()) THEN
        CONVERT(decimal(10, 4), DATEDIFF(year, DOB, GETDATE()) - 1) + 
        CONVERT(decimal(10, 4), DATEDIFF(day, DATEADD(year, DATEDIFF(year, DOB, GETDATE()) - 1, DOB), GETDATE())) / 
        CONVERT(decimal(10, 4), DATEDIFF(day, DATEADD(year, DATEDIFF(year, DOB, GETDATE()) - 1, DOB), DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB)))
    WHEN CONVERT(date, DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB)) < CONVERT(date, GETDATE()) THEN
        CONVERT(decimal(10, 4), DATEDIFF(year, DOB, GETDATE())) +
        CONVERT(decimal(10, 4), DATEDIFF(day, DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB), GETDATE())) / 
        CONVERT(decimal(10, 4), DATEDIFF(day, DATEADD(year, DATEDIFF(year, DOB, GETDATE()), DOB), DATEADD(year, DATEDIFF(year, DOB, GETDATE()) + 1, DOB)))
    ELSE
        CONVERT(decimal(10, 4), DATEDIFF(year, DOB, GETDATE()))
    END AS Age
FROM SomeTable

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question