Solved

Age Calculation in a sub Query

Posted on 2007-03-20
4
722 Views
Last Modified: 2008-01-09
Hello I am trying to add an age calculation within a sub querry but can not seem to get it in there.
select distinct cl.uniqueid_c,cl.clientcode_c,cl.firstname_vc,cl.lastname_vc, cd.diagnosis_c, ard.description_vc,
cd.axis_c, cd.type_c,ep.type_vc as eptype, ep.status_vc,ep.episodeiden_si,
need to add a subquery here to calculate age from the birthdate_d field. any help would be great


from ar.client cl
inner join cd.clientdiagnosis cd on cl.uniqueid_c = cd.clientid_c
inner join cd.episode ep on cl.uniqueid_c = ep.clientid_c
inner join ar.diagnosiscodes dc on dc.uniqueid_c = cd.diagnosiscodeid_c
inner join ar.diagnosis ard on ard.uniqueid_c = dc.diagnosisid_c
--inner join ar.cpsplan cp on cl.uniqueid_c = cp.clientid_c
--inner join ar.psplanmaster pm on cp.psplanmasterid_c = pm.uniqueid_c
where cl.termdate_d is null and ep.enddate_d is null
--and cd.enddate_d is null
--and cl.clientcode_c not like ('ds%')
--and cd.diagnosis_c in( '299.00') and cd.type_c = 'P'
order by cl.lastname_vc
--pm.pscode_c = '0002'
--and
--cd.diagnosis_c ='294.1'
0
Comment
Question by:mguptill
4 Comments
 
LVL 9

Expert Comment

by:dbeneit
ID: 18759718

can you use the function datediff:

datediff(year,birthdate_d,GETDATE())
0
 
LVL 11

Accepted Solution

by:
dready earned 500 total points
ID: 18760633
dbeneit: That will not work correctly, because it only substracts the years from now and birthdate, but doesnt take into account wether today is before or after the persons birthdate in this calendar year.

mgutpill:
You didnt say wether you want the age as an int, or also with some decimal precision.
You could try something like
DateDiff('d',[Date of Birth],getDate())/365.25 AS age
or, if you want an integer, you'd have to convert this to an int or round it.

another, slightly more complicated but precise way to get the age in years:
select datediff(yy, birthday, getDate()) -
      (case when (datepart(m, birthday) > datepart(m, getDate())) or
                  (datepart(m, birthday) = datepart(m, getDate()) And
                        datepart(d, birthday) > datepart(d, getDate()))
                  then 1
                  else 0
      end) as Age1

good luck,

dready
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 18767778
SELECT ...other columns as above...,
    DATEDIFF(YEAR, birthdate_d, GETDATE()) - CASE WHEN CONVERT(CHAR(5), birthdate_d, 1) >
        CONVERT(CHAR(5), GETDATE(), 1) THEN 1 ELSE 0 END AS [Age]
FROM ...rest same as above...
0
 

Author Comment

by:mguptill
ID: 18770417
dready thanks for your help I am sorry it took so long to accept I thought I accepted your solution the day I got it.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now