0 votes
1 view
in SQL by (20.3k points)

What I'm trying to do is use more than one CASE WHEN condition for the same column.

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)

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

1 Answer

0 votes
by (40.3k points)

Two formats of case expression are as follows:

Refer to this video to learn CASE EXPRESSION in detail:

You can use CASE with many WHEN like this;

CASE  WHEN Col1 = 1 OR Col3 = 1  THEN 1 

WHEN Col1 = 2 THEN 2

      ...

ELSE 0 END as Qty

The Simple CASE expression can be written like this:

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

Or else you can use CASE within CASE this way;

CASE  WHEN Col1 < 2 THEN  

      CASE Col2 WHEN 'X' THEN 10 ELSE 11 END

      WHEN Col1 = 2 THEN 2

      ...

      ELSE 0 END as Qty

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...