Case sentence using SQL - sql

I am using double case sentence to get a value from column in a table based on 2 conditions that are available in 2 other columns in same table , and else (otherwise) the function should give null when it is null or 0 when it is 0 .
Example of code is below :
CASE CODE
WHEN 'ABC'
CASE NAME WHEN 'XYZ'
THEN 'VALUE'
ELSE NULL
END
ELSE NULL
END
The problem is if I use NULL after else then it gives all ( both null values and values with 0 ) as NULL , or if I use 0 instead of NULL after ELSE then both null and 0 values are given as 0 .
I have tried to write the sentence in many ways but I dont know its not working . Hopefully somebody can give me some good solution regarding this.

CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END

I believe you want:
CASE WHEN CODE = 'ABC' AND NAME = 'XYZ' THEN 'VALUE' ELSE NULL END
This structure of the CASE statement is more flexible than sticking the field name before WHEN.

Are you trying to do something like this?
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN 'VALUE'
when code = '0' or name = '0'
then '0'
ELSE NULL
end)
Or perhaps it is this (based on the fact that you are trying to get something from a column):
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
when value is NULL or value = 0
then value
ELSE NULL
end)
Based on your comment, I think this will work:
(CASE when CODE = 'ABC' and NAME = 'XYZ'
THEN value
ELSE NULL
end)
Or is it this:
(CASE when CODE = 'ABC' and NAME = 'XYZ' and value <> '0'
THEN 'value'
when CODE = 'ABC' and NAME = 'XYZ' and value = '0'
then '0'
ELSE NULL
end)
However, I think this is equivalent to your original nested two-case version.

Related

Create Multiple Count Columns using Case When

I have to Case statements that count the same column just with a differnt criteria. The problem i am having is that the first case creates a null entry in the Test2 column but then counts it on my second case statement and leaves the Test 1 column null. I would like to have both counts side by side instead of created a duplicate row.
select m.no,
Case when itemtype = 'S' THEN count(ITEMKEY) end as Test1,
case when ItemType='C' THEN count(ITEMKEY) END as Test2
from test m
I'm pretty sure you want conditional aggregation. The case expression is an argument to the aggregation function:
select m.no,
sum(case when itemtype = 'S' then 1 else 0 end) as test1,
sum(case when itemtype = 'C' then 1 else 0 end) as test2
from test m
group by m.no;
This assumes that itemKey is never null, so the count() is just counting rows.
Following query can display records which having Itemtype 'S' or 'C' with count of itemkey. if itemkey is null it will display 0 else count of item key
select m.no,
Case when isnull( m.itemtype = 'S',0) THEN (select count(a.ITEMKEY) from test a where a.itemtype = 'S' ) else 0 end as Test1,
case when isnull( m.itemtype='C',0) THEN (select count(b.ITEMKEY) from test b where b.itemtype = 'C') else 0 END as Test2
from test m

How to use sum(case) with three conditions

I usually use sum(case) to get sum of some columns:
i.e. SUM(CASE WHEN over05 = 'OK' THEN 1 ELSE 0 END) AS OK_05
and this is perfect when I have a column with two values, but when I have a column where I have three values:
i.e. over 05 = '1' or 'X' or '2'
how can I do a sum(case)?
If you want all three values to return the same thing, you should use IN():
SUM(
CASE
WHEN over05 IN ('1', 'X', '2') THEN 1
ELSE 0 END
) AS OK_05
If you want each value to return something different, you should use multiple WHEN ... THEN :
SUM(
CASE
WHEN over05 = '1' THEN 1
WHEN over05 = 'X' THEN 2
WHEN over05 = '2' THEN 3
ELSE 0 END
) AS OK_05

how to take select query output of one coloumn in another column

SELECT CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end from v$database;
output:
name
pramod
i Want output as :
__name____|_other name____
pramod | ladkat
To get the expected result you have to use the following query and you can avoid else part in the case if you want to display data in matched condition only. You can also set NULL or '' in else to display NULL or Blank value in case of mismatch:
SELECT
CASE WHEN name='AUTHUAT' THEN 'pramod' ELSE 'ladkat' END name,
CASE WHEN name='AUTHUAT' THEN 'ladkat' ELSE 'pramod' END other_name
FROM v$database;
You're maybe looking for conditional aggregation ?
SELECT MAX(CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end) as name,
MAX(CASE WHEN name='AUTHUAT' then 'pramod' else 'ladkat' end) as other_name
from v$database;

Replacing CASE in SQL query

I got three columns with binary values. Then i got a query which makes a string based on those values. I made this work using case. But it's quite huge (it's a part of a bigger query), and i was wondering maybe there is a better way of doing this?
SELECT (CASE
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'CGR'
WHEN TableA.flag1 = 1 AND TableA.flag2 = 1 THEN 'CG'
WHEN TableA.flag1 = 1 AND TableA.flag3 = 1 THEN 'CR'
WHEN TableA.flag2 = 1 AND TableA.flag3 = 1 THEN 'GR'
WHEN TableA.flag1 = 1 THEN 'C'
WHEN TableA.flag2 = 1 THEN 'G'
WHEN TableA.flag3 = 1 THEN 'R'
ELSE 'nothing'
END)
FROM TableA
Im working on MSSQL 2000 server.
You can use left() instead.
select left('C', T.flag1)+
left('G', T.flag2)+
left('R', T.flag3)
from TableA as T
I don't know if this is "better" but it is more concise:
SELECT ((CASE WHEN TableA.flag1 = 1 THEN 'C' ELSE '' END) +
(CASE WHEN TableA.flag2 = 1 THEN 'G' ELSE '' END) +
(CASE WHEN TableA.flag3 = 1 THEN 'R' ELSE '' END)
)
FROM TableA;
Okay, this isn't exactly the same because you get '' instead of 'nothing'. But I think the empty string does a better job of representing "no flags" than 'nothing' does.
Try this:
SELECT
CASE WHEN flag1 = 1 THEN 'C' ELSE '' END +
CASE WHEN flag2 = 1 THEN 'G' ELSE '' END +
CASE WHEN flag3 = 1 THEN 'R' ELSE '' END
FROM TABLEA
Maybe you could use function and move this CASE statement to this function

transforming from 'Y' or 'N' to bit

I have a table which has a column called Direct of type char(1). It's values are either 'Y' or 'N' or NULL. I am creating a view and I want the value to be transformed to either 0 or 1 of type bit. Right now it's of type INT. How do I go about doing this?
Following is the code:
CASE WHEN Direct = 'Y' THEN (SELECT 1)
WHEN Direct <> 'Y' THEN (SELECT 0) END AS DirectDebit
EDIT: How can I make sure the column type is of type BIT?
This will get you your bit..
CAST(CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END AS BIT) AS DirectDebit
See if this works:
SELECT CASE WHEN Direct = 'Y' THEN 1 ELSE 0 END FROM YOURTABLE
SELECT CASE Direct
WHEN 'Y' THEN '1'
WHEN 'N' THEN '0'
ELSE '0'
END as DirectDebit
FROM TableName
... should work.