Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Age Calculation in a sub Query

Posted on 2007-03-20
4
Medium Priority
?
743 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 2000 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 70

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

721 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