?
Solved

Nested if then else statements

Posted on 2012-09-17
3
Medium Priority
?
2,000 Views
Last Modified: 2012-09-17
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
Comment
Question by:kwcowboy195612
  • 2
3 Comments
 
LVL 8

Expert Comment

by:stalhw
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

Open in new window

0
 
LVL 8

Expert Comment

by:SNeupane
ID: 38406477
In your original statement, just use 'IN' instead of multiple 'OR's.
0
 
LVL 8

Accepted Solution

by:
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))

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question