Solved

Age Calculation in a sub Query

Posted on 2007-03-20
4
736 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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
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.

679 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