How do I do multiple CASE WHEN conditions using SQL Server 2008

0 votes

For the same column, I'm attempting to use multiple CASE WHEN conditions.

Here is my code for the query:

   SELECT   Url='',
            p.ArtNo,
            p.[Description],
            p.Specification,
            CASE 
            WHEN 1 = 1 or 1 = 1 
               THEN 1 
               ELSE 0 
            END as Qty,
            p.NetPrice,
            [Status] = 0
      FROM  Product p (NOLOCK)

What I want to do is use more than one WHEN for the same column "qty".

As in the following code:

IF
// CODE
ELSE IF
// CODE
ELSE IF
// CODE
ELSE
// CODE

Can someone please help me with this?

Sep 19 in Database by Kithuzzz
• 20,660 points
59 views

1 answer to this question.

0 votes

There are three formats of the case expression. You can do CASE with many WHEN as;

CASE  WHEN Col1 = 1 OR Col3 = 1  THEN 1 
      WHEN Col1 = 2 THEN 2
      ...
      ELSE 0 END as Qty

Or a Simple CASE expression

CASE Col1 WHEN 1 THEN 11 WHEN 2 THEN 21 ELSE 13 END

Or CASE within CASE as;

CASE  WHEN Col1 < 2 THEN  
                    CASE Col2 WHEN 'X' THEN 10 ELSE 11 END
      WHEN Col1 = 2 THEN 2
      ...
      ELSE 0 END as Qty
answered Sep 20 by narikkadan
• 37,660 points

Related Questions In Database

0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3 in Database by Vaani
• 7,020 points
168 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4 in Database by Vaani
• 7,020 points
71 views
0 votes
0 answers

How can I delete using INNER JOIN with SQL Server?

In SQL Server 2008, I want to ...READ MORE

Aug 23 in Database by Kithuzzz
• 20,660 points
97 views
0 votes
1 answer

How to delete duplicate rows in SQL Server?

To answer your query, note that CTEs ...READ MORE

answered Feb 10 in Others by Soham
• 9,670 points
268 views
0 votes
1 answer

Rename column SQL Server 2008

Use sp_rename EXEC sp_RENAME 'TableName.PreviousColumnName' , 'NewColumnName', 'COLUMN' See: SQL SERVER ...READ MORE

answered Feb 23 in Database by Vaani
• 7,020 points
100 views
0 votes
0 answers

How to update Identity Column in SQL Server?

With 200 records currently, I want to ...READ MORE

Aug 9 in Database by Kithuzzz
• 20,660 points
390 views
0 votes
0 answers

Query to check index on a table

I want a query to see if ...READ MORE

Aug 11 in Database by Kithuzzz
• 20,660 points
47 views
0 votes
1 answer

How do I obtain a Query Execution Plan in SQL Server?

There are several ways to get an ...READ MORE

answered Sep 20 in Database by narikkadan
• 37,660 points
87 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP