How do I do multiple CASE WHEN conditions using SQL Server 2008?
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 then one WHEN for the same column "qty".
As in the following code:
IF
// CODE
ELSE IF
// CODE
ELSE IF
// CODE
ELSE
// CODE
There are two formats of 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
Just use this one, You have to use more when they are classes.
SELECT Url='',
p.ArtNo,
p.[Description],
p.Specification,
CASE
WHEN 1 = 1 or 1 = 1
THEN 1
WHEN 2 = 2
THEN 2
WHEN 3 = 3
THEN 3
ELSE 0
END as Qty,
p.NetPrice,
[Status] = 0
FROM Product p (NOLOCK)
You can use below example of case when with multiple conditions.
SELECT
id,stud_name,
CASE
WHEN marks <= 40 THEN 'Bad'
WHEN (marks >= 40 AND
marks <= 100) THEN 'good'
ELSE 'best'
END AS Grade
FROM Result
Something Like this, Two Conditions Two Columns
SELECT ITEMSREQ.ITEM AS ITEM,
ITEMSREQ.CANTIDAD AS CANTIDAD,
(CASE WHEN ITEMSREQ.ITEMAPROBADO=1 THEN 'APROBADO'
WHEN ITEMSREQ.ITEMAPROBADO=0 THEN 'NO APROBADO'
END) AS ITEMS,
(CASE
WHEN ITEMSREQ.ITEMAPROBADO = 0
THEN CASE WHEN REQUISICIONES.RECIBIDA IS NULL THEN 'ITEM NO APROBADO PARA ENTREGA' END
WHEN ITEMSREQ.ITEMAPROBADO = 1
THEN CASE WHEN REQUISICIONES.RECIBIDA IS NULL THEN 'ITEM AUN NO RECIBIDO'
WHEN REQUISICIONES.RECIBIDA=1 THEN 'RECIBIDO'
WHEN REQUISICIONES.RECIBIDA=0 THEN 'NO RECIBIDO'
END
END)
AS RECIBIDA
FROM ITEMSREQ
INNER JOIN REQUISICIONES ON
ITEMSREQ.CNSREQ = REQUISICIONES.CNSREQ
case
when a.REASONID in ('02','03','04','05','06') then
case b.CALSOC
when '1' then 'yes'
when '2' then 'no'
else 'no'
end
else 'no'
end