rschmalt
asked on
Nested CASE/IF statements in MS SQL Server 2000
I'm writing a SQL Query in VB2003 against an MS SQL Server 2000 database. Basically, I"m trying to replicate an Access query (which works) in MS SQL language. The problem is, I can't seem to make a nested CASE statement work.
Here is my original query:
[code] SELECT Left(dbo_porel.jobnum,5) AS ShortJobNum,
IIf(dbo_porel.jobseqtype=" M",IIf(Len (nz(dbo_pa rtclass.sh ortchar01, ""))=0,"Un assigned", dbo_partcl ass.shortc har01),
IIf(Len(nz(dbo_opmaster.sh ortchar01, ""))=0,"Un assigned", dbo_opmast er.shortch ar01)) as Headers,
(dbo_porel.relqty*dbo_pode tail.unitc ost) AS Cost
FROM (((dbo_porel
INNER JOIN dbo_podetail ON (dbo_porel.ponum = dbo_podetail.ponum) AND (dbo_porel.poline = dbo_podetail.poline))
LEFT JOIN dbo_joboper ON (dbo_porel.jobnum = dbo_joboper.jobnum) AND (dbo_porel.assemblyseq = dbo_joboper.assemblyseq) AND (dbo_porel.jobseq = dbo_joboper.oprseq))
LEFT JOIN dbo_opmaster ON dbo_joboper.opcode = dbo_opmaster.opcode)
LEFT JOIN dbo_partclass ON dbo_podetail.classid = dbo_partclass.classid
WHERE Left(dbo_porel.jobnum,5) Between "36640" And "36646" [/code]
Specifically, I'm dealing with this line:
[code]
IIf(dbo_porel.jobseqtype=" M",IIf(Len (nz(dbo_pa rtclass.sh ortchar01, ""))=0,"Un assigned", dbo_partcl ass.shortc har01),
IIf(Len(nz(dbo_opmaster.sh ortchar01, ""))=0,"Un assigned", dbo_opmast er.shortch ar01)) as Headers[/code]
I'm trying to rewrite this in something MS SQL will luck and frankly, I"ve been without much success. My latest attempt was as follows:
[code]
Headers = Case
WHEN porel.jobseqtype='M' THEN
(WHEN Len(IsNull(partclass.short char01,'') )=0 THEN 'Unassigned'
Else partclass.shortchar01)
ELSE
(WHEN Len(IsNull(opmaster.shortc har01,'')) =0 THEN 'Unassigned'
ELSE opmaster.shortchar01)
End[/code]
I'd sure appreciate someone's assistance on this. All my searches so far have not indicated how to do the following:
If Value
THEN
IF Value
THEN
ELSE
THEN
ELSE
IF Value
THEN
ELSE
THEN
END IF
Thank you!
Here is my original query:
[code] SELECT Left(dbo_porel.jobnum,5) AS ShortJobNum,
IIf(dbo_porel.jobseqtype="
IIf(Len(nz(dbo_opmaster.sh
(dbo_porel.relqty*dbo_pode
FROM (((dbo_porel
INNER JOIN dbo_podetail ON (dbo_porel.ponum = dbo_podetail.ponum) AND (dbo_porel.poline = dbo_podetail.poline))
LEFT JOIN dbo_joboper ON (dbo_porel.jobnum = dbo_joboper.jobnum) AND (dbo_porel.assemblyseq = dbo_joboper.assemblyseq) AND (dbo_porel.jobseq = dbo_joboper.oprseq))
LEFT JOIN dbo_opmaster ON dbo_joboper.opcode = dbo_opmaster.opcode)
LEFT JOIN dbo_partclass ON dbo_podetail.classid = dbo_partclass.classid
WHERE Left(dbo_porel.jobnum,5) Between "36640" And "36646" [/code]
Specifically, I'm dealing with this line:
[code]
IIf(dbo_porel.jobseqtype="
IIf(Len(nz(dbo_opmaster.sh
I'm trying to rewrite this in something MS SQL will luck and frankly, I"ve been without much success. My latest attempt was as follows:
[code]
Headers = Case
WHEN porel.jobseqtype='M' THEN
(WHEN Len(IsNull(partclass.short
Else partclass.shortchar01)
ELSE
(WHEN Len(IsNull(opmaster.shortc
ELSE opmaster.shortchar01)
End[/code]
I'd sure appreciate someone's assistance on this. All my searches so far have not indicated how to do the following:
If Value
THEN
IF Value
THEN
ELSE
THEN
ELSE
IF Value
THEN
ELSE
THEN
END IF
Thank you!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
indeed... sorry :)
ASKER
Nothing to be sorry about. :) LIke I said, you got me 99% of the way there. I'm just not sure how to assign the points considering I'm pretty new at this. ;)
if using sql 2000, i would also suggest creating a user defined function to handle the case statement.
user-defined functions can have _very_ bad impact on the performance of the query, so they have be used with big caution, especially so for something simple.
ASKER
WHEN porel.jobseqtype='M'
THEN CASE
WHEN Len(IsNull(partclass.short
THEN 'Unassigned'
ELSE partclass.shortchar01
END
ELSE CASE
WHEN Len(IsNull(opmaster.shortc
THEN 'Unassigned'
ELSE opmaster.shortchar01
END
END
----------------
This code works! Thanks! AngelIII - you got me 99% of the way there, but vadimrapp1 's actual syntax is what saved me (angelIII's code was missing CASE).
How do I assign the points?