Solved

How do I Nest cases or If in CASE MS SQL

Posted on 2010-09-07
9
417 Views
Last Modified: 2012-05-10
I have the following condition in my query
WHERE      StartDate>=@UserDate
      AND
      CASE @Type
            WHEN 1 THEN      CASE WHEN  yearCol  =  @year THEN 1 ELSE 0 END
            WHEN 2 THEN       CASE WHEN  color<>@Color THEN 1 ELSE 0 END
            END = 1
In some case I have @Tpype=2 and @color=null
What I would like to do in the second statement (when 2 then..) check to see if @color is null where I will do something like this
CASE @color
     When null THEN CASE color<>@NewColor THEN 1 ELSE 0 END
     When not Null Then CASE color <> @Color THEN 1 ELSE 0 END
END=1
The above syntax is incorrect what's the right syntax please note that what I am doing in those cases is executing a condition based on the case
Thank you
0
Comment
Question by:xav056
  • 3
  • 3
  • 3
9 Comments
 
LVL 39

Expert Comment

by:appari
ID: 33623952
try this

WHERE      StartDate>=@UserDate
      AND
      CASE @Type
            WHEN 1 THEN      CASE WHEN  yearCol  =  @year THEN 1 ELSE 0 END
            WHEN 2 THEN      
      CASE
           When @color is null THEN CASE color<>@NewColor THEN 1 ELSE 0 END
                When @color is not Null Then CASE color <> @Color THEN 1 ELSE 0 END
      end
END=1
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 33623966
Or flat without nesting

WHERE StartDate>=@UserDate
      AND
      CASE  WHEN @Type=1 AND yearCol = @year THEN 1
            WHEN @Type=1 THEN 0
            WHEN @Type=2 AND @color IS NOT NULL AND color<>@Color THEN 1
            WHEN @Type=2 AND @color IS NOT NULL THEN 0
            WHEN @Type=2 AND @color IS NULL AND color<>@NewColor THEN 1
            WHEN @Type=2 AND @color IS NULL THEN 0
      END = 1
0
 
LVL 9

Author Comment

by:xav056
ID: 33623971
incorrect syntax wiggly lines under the <> anf then
e.GDID<>@GDID (incorrect syntax bear <> and incorrect syntax near then

Thank you
0
 
LVL 39

Expert Comment

by:appari
ID: 33623982
>>incorrect syntax wiggly lines under the <> anf then
>>e.GDID<>@GDID (incorrect syntax bear <> and incorrect syntax near then
seems the error is now from a different place. post your complete query.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Author Comment

by:xav056
ID: 33624000
appari
nothing before just
Select *
from VehInfo
WHERE      StartDate>=@UserDate
      AND
      CASE @Type
            WHEN 1 THEN      CASE WHEN  yearCol  =  @year THEN 1 ELSE 0 END
            WHEN 2 THEN      
      CASE
           When @color is null THEN CASE color<>@NewColor THEN 1 ELSE 0 END
                When @color is not Null Then CASE color <> @Color THEN 1 ELSE 0 END
      end
END=1
0
 
LVL 39

Assisted Solution

by:appari
appari earned 250 total points
ID: 33624007
missed when,

Select *
from VehInfo
WHERE      StartDate>=@UserDate
      AND
      CASE @Type
            WHEN 1 THEN      CASE WHEN  yearCol  =  @year THEN 1 ELSE 0 END
            WHEN 2 THEN      
      CASE
           When @color is null THEN CASE when color<>@NewColor THEN 1 ELSE 0 END
                When @color is not Null Then CASE when color <> @Color THEN 1 ELSE 0 END
      end
END=1
0
 
LVL 9

Author Closing Comment

by:xav056
ID: 33624024
Thank you both
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33624066
A properly nested CASE statement - 3 levels, can become really confusing

      CASE
      WHEN @Type=1 THEN
               CASE WHEN yearCol = @year THEN 1 ELSE 0 END
      WHEN @Type=2 THEN
           CASE
           WHEN @color IS NOT NULL THEN
                CASE WHEN color<>@Color THEN 1 ELSE 0 END
           ELSE -- the complement is clearly null
                CASE WHEN color<>@NewColor THEN 1 ELSE 0 END
           END
      END = 1
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33624076
Sorry was distracted, here's part 3 of 3.
For your case, you only want @newcolor when @color is null, you can use a better technique instead of CASE

      CASE
      WHEN @Type=1 THEN
           CASE WHEN yearCol = @year THEN 1 ELSE 0 END
      WHEN @Type=2 THEN
           CASE WHEN color<>Coalesce(@Color,@NewColor) THEN 1 ELSE 0 END
      END = 1
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now