Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Having trouble with CASE command

Status
Not open for further replies.

gjmac2

IS-IT--Management
Mar 31, 2006
45
US
i am trying to populate a column based on certain criteria using a CASE command, and I am getting "ADO error: Invalid syntax near the keyword 'AS'.

Here is the code:

CASE WHEN DBO.VW_RPTELIGIBLEORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND
dbo.VW_REferralActivity.organdisp = 'Family Declined' OR
dbo.VW_REferralActivity.organdisp = 'KNOWN PRIOR OBJECTION' THEN 'ED' else
CASE WHEN DBO.VW_RPTPotentialORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND
dbo.VW_REferralActivity.organdisp <> 'Family Declined' OR
dbo.VW_REferralActivity.organdisp <> 'KNOWN PRIOR OBJECTION' or
dbo.VW_REferralActivity.organdisp <> 'recovered' THEN 'PD' else null END AS donortype,

Anyone have an idea what I'm doing wrong?
 
first glance Id say you have two case statements and only one end.
 
you cant have an embedded case i.e. you need to have an END with every CASE Statement

e.g.
Code:
CASE WHEN 
	DBO.VW_RPTELIGIBLEORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' 
		AND (dbo.VW_REferralActivity.organdisp = 'Family Declined' 
			OR dbo.VW_REferralActivity.organdisp = 'KNOWN PRIOR OBJECTION') THEN 'ED' 
	WHEN DBO.VW_RPTPotentialORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND 
                      (dbo.VW_REferralActivity.organdisp <> 'Family Declined' OR
                      dbo.VW_REferralActivity.organdisp <> 'KNOWN PRIOR OBJECTION' or
                      dbo.VW_REferralActivity.organdisp <> 'recovered') THEN 'PD' 
	else null END AS donortype,

"I'm living so far beyond my income that we may almost be said to be living apart
 
try this:
Code:
CASE WHEN DBO.VW_RPTELIGIBLEORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND dbo.VW_REferralActivity.organdisp = 'Family Declined' OR dbo.VW_REferralActivity.organdisp = 'KNOWN PRIOR OBJECTION' THEN 'ED' 
     WHEN DBO.VW_RPTPotentialORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND dbo.VW_REferralActivity.organdisp <> 'Family Declined' OR dbo.VW_REferralActivity.organdisp <> 'KNOWN PRIOR OBJECTION' or dbo.VW_REferralActivity.organdisp <> 'recovered' THEN 'PD' 
     else null END AS donortype
 
Thank you all very much. It works now. I took out the second CASE command, and it works.

Thanks again.
 
Shouldn't you group the 2 OR clauses as well otherwise it'll pick the sceond one up regardless of REFERRALTYPE

Code:
CASE WHEN DBO.VW_RPTELIGIBLEORGANDONOR.REFERRALTYPE = 'REGULARREFERRAL' AND 
                      [COLOR=red]([/color]dbo.VW_REferralActivity.organdisp = 'Family Declined' OR
                      dbo.VW_REferralActivity.organdisp = 'KNOWN PRIOR OBJECTION'[COLOR=red])[/color] THEN 'ED' else

and the same for the OR in the second half.

M.
 
Sorry - just read back and realised hmckillop raised the above in his code. Thank god it's a bank holiday weekend here in the UK!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top