Solved

Age Calculation in a sub Query

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

630 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