?
Solved

How do I Nest cases or If in CASE MS SQL

Posted on 2010-09-07
9
Medium Priority
?
455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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
 
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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

764 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