Solved

How do I Nest cases or If in CASE MS SQL

Posted on 2010-09-07
9
441 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Pivot Rows To Columns 10 65
kill process lock Sql server 9 63
get column names from table in vb.net 8 49
database level memory cache..? 8 31
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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