Tech2377
Programmer
- Nov 13, 2007
- 81
I want to Add in a Case Statement to my Stored Procedure to Identify 5 classifications:
A: Birth to 6 yrs old
B: Female 7 yrs to 18 yrs
C: Male 7 yrs to 18 yrs
D: Female over 18 yrs old
E: Male over 18 yrs old
Any help is appreciated!
I tried using this:
and I get this:
A: Birth to 6 yrs old
B: Female 7 yrs to 18 yrs
C: Male 7 yrs to 18 yrs
D: Female over 18 yrs old
E: Male over 18 yrs old
Any help is appreciated!
Code:
Declare @ApptDate datetime
Select @ApptDate = a.ApptStart
FROM Appointments a
WHERE a.AppointmentsId = 1095
SELECT '290.PatientName'=IsNull(pp.First,'') + ' ' + isnull(pp.Middle,'') + ' ' + isnull(pp.Last,'')+ ' ' + isnull(pp.Suffix,''),
'291.PatLast'=IsNull(pp.Last,''),
'292.PatFirst'=IsNull(pp.First,''),
'293.PatMiddle'=IsNull(pp.Middle,''),
'294.PatientAddr1'=IsNull(pp.Address1,''),
'295.PatientAddr2'=IsNull(pp.Address2,''),
'296.PatientCity'=IsNull(pp.City,''),
'297.PatientState'=IsNull(pp.State,''),
'298.PatientZip'=IsNull(pp.Zip,''),
'299.PatientCountry' = ISNULL(pp.Country,''),
'300.PatientBirthdate' = pp.Birthdate,
'301.PatientSex'=IsNull(pp.Sex,''),
'302.PatientPhone1'=IsNull(pp.Phone1,''),
'303.PatientSSN'=IsNull(pp.SSN,''),
'304.PatOccupation'=IsNull(pp.EmpOccup,''),
'305.PatSchool'=IsNull(pp.MedicalRecordNumber,''),
'306.PatBudget'=IsNull(g.Budget,0),
'307.PatientSameAsGuarantor'=IsNull(pp.PatientSameAsGuarantor,0),
'308.PatSuffix'=IsNull(pp.Suffix,''),
'400.PatientAge' = CASE WHEN datediff(day, pp.birthdate, @ApptDate) <= 6 THEN cast(datediff(day, pp.birthdate, @ApptDate) as varchar(10)) + ' dys'
WHEN datediff(day, pp.birthdate, @ApptDate) <= 112 THEN cast(floor(cast(datediff(day, pp.birthdate, @ApptDate) as decimal) / 7) as varchar(10)) + ' wks'
WHEN floor(cast(datediff(day, pp.birthdate, @ApptDate) as decimal) / 365.25) < 2 and day(pp.birthdate) <= day(@ApptDate) THEN cast(datediff(month,pp.birthdate, @ApptDate) as varchar(10)) + ' mos'
WHEN floor(cast(datediff(day, pp.birthdate, @ApptDate) as decimal) / 365.25) < 2 and day(pp.birthdate) > day(@ApptDate) THEN cast(datediff(month,pp.birthdate, @ApptDate) - 1 as varchar(10)) + ' mos'
WHEN floor(cast(datediff(day, pp.birthdate, @ApptDate) as decimal) / 365.25) >= 2 THEN cast(floor(cast(datediff(day, pp.birthdate, @ApptDate) as decimal) / 365.25) as varchar(10)) + ' yrs'
ELSE '' END
FROM PatientProfile pp
LEFT JOIN Guarantor g ON pp.GuarantorId = g.GuarantorId
WHERE pp.PatientProfileID = 319
I tried using this:
Code:
'401.PatientAgeGroup' =
CASE
WHEN [400.PatientAge] > 18 AND pp.PatientSex = 'M' THEN 'E'
WHEN [400.PatientAge] > 18 AND pp.PatientSex = 'F' THEN 'D'
WHEN [400.PatientAge] BETWEEN 7 AND 18 AND pp.PatientSex = 'M' THEN 'C'
WHEN [400.PatientAge] BETWEEN 7 AND 18 AND pp.PatientSex = 'F' THEN 'B'
WHEN [400.PatientAge] BETWEEN 0 AND 6 THEN 'A'
ELSE 'UNKNOWN'
END
and I get this:
Code:
Msg 207, Level 16, State 1, Line 36
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'PatientSex'.
Msg 207, Level 16, State 1, Line 37
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 37
Invalid column name 'PatientSex'.
Msg 207, Level 16, State 1, Line 38
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 38
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 38
Invalid column name 'PatientSex'.
Msg 207, Level 16, State 1, Line 39
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 39
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 39
Invalid column name 'PatientSex'.
Msg 207, Level 16, State 1, Line 40
Invalid column name '400.PatientAge'.
Msg 207, Level 16, State 1, Line 40
Invalid column name '400.PatientAge'.