Solved

How do I Nest cases or If in CASE MS SQL

Posted on 2010-09-07
9
449 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 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tempdb log keep growing 7 54
SQL server client app 3 72
SQL 2008 - Disappearing Temp Table in Stored Procedure 24 58
Need more granular date groupings 4 44
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…

737 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