Solved

Age Calculation in a sub Query

Posted on 2007-03-20
4
726 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:Scott Pletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

863 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

20 Experts available now in Live!

Get 1:1 Help Now