Age at Time of Service - SQL 2005

I need help with the following field:

'Age @ TOS' = DATEDIFF (YY, patientprofile.birthdate, patientvisitprocs.dateofservicefrom),

PatientProfile.Birthdate - 01/01/2001
PatientVisitProcs.dateofservicefrom = 07/01/2001

In my report, I get a '0' as my Age @ TOS. What I would like to have is 7 months vs the '0' it is reporting. I would like to try accomplishing this without doing a function if possible. I know I used YY in my DATEDIFF. I only noticed this today because of a few patients in my DB were under 1 yr at the TOS.

LVL 7
Jeff SAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TextReportCommented:
Example below. cheers, Andrew
'Age @ TOS' = 
    CASE WHEN DATEDIFF(Year, , patientprofile.birthdate, patientvisitprocs.dateofservicefrom) = 0
         THEN DATEDIFF(Month, , patientprofile.birthdate, patientvisitprocs.dateofservicefrom)
         END

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
David ToddSenior DBACommented:
Hi,

There appears to be a couple fo extra comma's in Andrews code ...
should be
DATEDIFF( Year, patientprofile.birthdate, patientvisitprocs.dateofservicefrom )
etc ...

Cheers
  David

PS Is 'Age @ TOS' returning a string or a number? That is, the then needs to append a ' month(s)' to the result.

Do be aware of the edges problem with datediff as illustrated by the code snippet. Note that the actual time difference is only 1 second, yet every unit (apart from weeks) returns 1
use tempDb
go
 
declare @StartDateTime as DateTime
declare @EndDateTime as DateTime
 
set @StartDateTime = '2007-12-31 23:59:59'
set @EndDateTime = '2008-01-01 00:00:00'
 
set nocount on
select @StartDateTime, @EndDateTime
 
select datediff( year, @StartDateTime, @EndDateTime ) as Years
select datediff( quarter, @StartDateTime, @EndDateTime ) as Quarters
select datediff( month, @StartDateTime, @EndDateTime ) as Months
select datediff( week, @StartDateTime, @EndDateTime ) as Weeks
select datediff( day, @StartDateTime, @EndDateTime ) as Days
select datediff( hour, @StartDateTime, @EndDateTime ) as Hours
select datediff( minute, @StartDateTime, @EndDateTime ) as Minutes
select datediff( second, @StartDateTime, @EndDateTime ) as Seconds

Open in new window

0
Jeff SAuthor Commented:
Hi David -

I noticed the second comma in Andrew's code and the ELSE .... however his technique got me going in the right direction.

I am now getting this error with the code in my snippet:

Server: Msg 245, Level 16, State 1, Line 115
Syntax error converting the varchar value '6 Months' to a column of data type int.


'Age @ TOS' = 
     	CASE WHEN DATEDIFF(YY,patientprofile.birthdate, patientvisitprocs.dateofservicefrom) <= 0
        THEN CAST(DATEDIFF(MM,patientprofile.birthdate, patientvisitprocs.dateofservicefrom)as varchar(10))+ ' Months'
        ELSE DATEDIFF(YY,patientprofile.birthdate, patientvisitprocs.dateofservicefrom)
        END,

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Jeff SAuthor Commented:
Ok I got it...


'Age @ TOS' = CASE
     	WHEN DATEDIFF(YY,patientprofile.birthdate, patientvisitprocs.dateofservicefrom) <= 0
        THEN CAST(DATEDIFF(MM,patientprofile.birthdate, patientvisitprocs.dateofservicefrom)as varchar(10))+ ' Months'
        ELSE CAST(DATEDIFF(YY,patientprofile.birthdate, patientvisitprocs.dateofservicefrom)as varchar(10))+ ' Yr'
        END,

Open in new window

0
Jeff SAuthor Commented:
Thanks again
0
Jeff SAuthor Commented:
Split points to be fair.
0
TextReportCommented:
Dear JeffSturgeon2002, you should be able to do this yourself by accepting 1 answer and assigning an assist.
Cheers, Andrew
0
Jeff SAuthor Commented:
ya I did ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.