Solved

Nested if then else statements

Posted on 2012-09-17
Medium Priority
2,000 Views
I want to find out if there is a better way to do this statement and make it work.

IF ( [Student Center].[Current Building] < '20' ) THEN
(IF ( [Course Code - Course]  = '0021' or
[Course Code - Course]  = '0025'  or
[Course Code - Course]  = '1711L'  or
[Course Code - Course]  = ' 1721' or
[Course Code - Course]  = '1731' or
[Course Code - Course]  = '1741'  or
[Course Code - Course]  = '8452'  or
[Course Code - Course]  = '8453' ) THEN
([Course Code - Course])  ELSE (null))
ELSE
(IF ( [Course Code - Course]  = 'J603' or
[Course Code - Course]  = 'J603L' or
[Course Code - Course]  = 'J605' or
[Course Code - Course]  = 'J608' or
[Course Code - Course]  = 'J615' or
[Course Code - Course]  = 'J615L'  or
[Course Code - Course]  = 'J703' or
[Course Code - Course]  = 'J705' or
[Course Code - Course]  = 'J715' or
[Course Code - Course]  = 'J715L'  or
[Course Code - Course]  = 'J709'  or
[Course Code - Course]  = 'J805' or
[Course Code - Course]  = 'J815' or
[Course Code - Course]  = 'J815L'  or
[Course Code - Course]  = 'J835') THEN
( [Course Code - Course])  ELSE (null))
0
Question by:kwcowboy195612
• 2

LVL 8

Expert Comment

ID: 38406228
I'm pretty sure a CASE statement would be more efficient...
``````declare @result varchar(20)

SELECT @result=CASE
WHEN [Student Center].[Current Building] < '20'
AND [Course Code - Course]  IN ('0021','0025','1711L','1721','1731','1741','8452','8453'
THEN [Course Code - Course]
WHEN NOT ([Student Center].[Current Building] < '20' )
AND [Course Code - Course] IN ('J603','J603L','J605','J608','J615','J615L' ,'J703','J705','J715','J715L' ,'J709' ,'J805','J815','J815L' ,'J835') THEN [Course Code - Course]
ELSE NULL END
``````
0

LVL 8

Expert Comment

ID: 38406477
0

LVL 8

Accepted Solution

stalhw earned 2000 total points
ID: 38406490
If you dont like CASE (not sure if it's possible in your context)
Like SNeupane said, you can just use IN:
``````IF [Student Center].[Current Building] < '20'  THEN
(IF [Course Code - Course]  IN ('0021','0025','1711L','1721','1731','1741','8452','8453' ) THEN
[Course Code - Course] ELSE (null))
ELSE
(IF [Course Code - Course] IN ('J603','J603L','J605','J608','J615','J615L' ,'J703','J705','J715','J715L' ,'J709' ,'J805','J815','J815L' ,'J835') THEN
[Course Code - Course] ELSE (null))
``````
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Make the most of your online learning experience.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression
Suggested Courses
Course of the Month17 days, 1 hour left to enroll