views:

125

answers:

4

In C# we can write

switch(num)
{
  case 0:
  case 1: // do something; break;
  case 2:
  ............
  ...........
  case n: // do something break;
  default: //do something; break;
}

How can I achieve the similar kind of stuff in SQL SERVER ?

I am not talking about the simple way of writing CASE in SQL SERVER. I am talking about if I need to perform the same king of operation in 2 or more case's as what I showed in C# code snippet, how to do the similar kind of stuff in SQL's CASE?

EDIT:

I got some nice answers from here already. How can I convert the following

SELECT CASE 
         WHEN [A]= num THEN '-' ELSE '' END [A], 
         CASE WHEN [B]= num THEN '-' ELSE '' END [B], 
         CASE WHEN [C]= num THEN '-' ELSE '' END [C],
         CASE WHEN [D]= num THEN '-' ELSE '' END [D]

...into something like:

SELECT CASE WHEN [A], 
       CASE WHEN [B], 
       CASE WHEN [C], 
       CASE WHEN [D] = num THEN '-' ELSE '' END [A] or [B] or [C] or [D]

Actually I need this in a PIVOT query. Last night I solved the problem. But I am not convinced with this way of writing. Because everytime, I am doing the same thing. So is there any better way of presenting this?

+1  A: 

Like this:

SELECT 
  CASE num
    WHEN 0 THEN ...
    WHEN 1 THEN ...
    ELSE ...
  END as SomeCol
FROM ...
Rob Farley
Although - I'm wondering if I'm missing something. In your C# code, you only handle one CASE, just like in this query. If you're wanting procedural code, then you should use IF, but you should try to rework your code into a single query if you can.
Rob Farley
I think he's talking about CASE WHEN num = 1 OR num = 2 THEN ...
Matt Hamilton
No no.. I am not looking for this.. It is perfect but I want like writing a 1-1 THEN's for a WHEN, can we do something likeCASE numWhen 0When 1 THEN....Is it possible?
priyanka.sarkar
Yes ..somthing similar to Mr. Hamilton's. To be honest why I need is last night I was writing a program using PIVOT. Now there I was transforming more than 1 columns and I was converting 1's to '-'. So what I did is, for every case say for 1 or 2 or 3 etc. I wrote the same piece of code. And I felt bad. So I was thinking if there is a better workaround
priyanka.sarkar
Ah, so you want:WHEN num IN ([A],[B],[C],[D]) THEN ...
Rob Farley
+1  A: 

SQL does support a CASE statement but it's not the same thing as the switch statement in high level languages like C# and Java. In a switch statement, you have the concept of fall-through where if a break statement is not encountered, the flow continues to the next case. In contrast, a SQL CASE statement behaves like a high level language's if(value==1){ ... }else if(value==2){ ... }else{ ... }.

Asaph
Yes you are right. I also searched the same in the net but with no answer. So I posted here just to ensure that. Thanks a lot.
priyanka.sarkar
+3  A: 

You might be looking for this.

SELECT
      CASE 
         WHEN (num BETWEEN 0 AND 2) THEN 'Between 0 and 2'
         WHEN (num = 3) THEN '3'
         ELSE 'Something else'
      END
...

More information on CASE from MSDN.

Michael Petrotta
I understand .. but how put the following into your formatSELECT CASE WHEN [A]= num THEN '-' ELSE '' END [A] ,CASE WHEN [B]= num THEN 'X' ELSE '' END [B] ,CASE WHEN [C]= num THEN 'X' ELSE '' END [C] ,CASE WHEN [D]= num THEN 'X' ELSE '' END [D]
priyanka.sarkar
I think you've got it as close as you can get it, if you want to return four separate columns.
Michael Petrotta
+1  A: 

Like C# SWITCH statement, SQL Server's CASE expression does not support fallthrough.

Notice the emphasis on expression - it's not to be used for control of flow, use what is listed here.

OMG Ponies