SQL query with multiple/embedded CASE - sql-server-2012

I am stuck in a query; if you have time can you let me know how to fix it?
I have a (hypothetical) table with TypeID, SubTypeID, Option1, Option2 and Option3 columns; last three columns are Boolean.
So, I might have something like this:
TypeID SubTypeID Option1 Option2 Option3
1 5 false false false
2 0 true false false
2 0 false true true
2 0 true true true
What I am trying to get, in case TypeID=2 is the following, using example above:
'Option 1'
'Option 2, Option 3'
'Option1, Option 2, Option 3'
I tried this but getting syntax error and I am not even sure it is correct:
case when fd.TypeID=1 then ft.SubType else (case when fd.Option1=1 then 'Option 1,' else (case when fd.Option2=1 then 'Option 2,' else (case when fd.Option3=1 then 'Option 3' else '' ))) as SubType,

You could use IIF/CASE:
SELECT
IIF(Option1='true', 'Option 1,', '') +
IIF(Option2='true', 'Option 2,', '') +
IIF(Option3='true', 'Option 3,', '') AS Output
FROM table_name
WHERE TypeId = 2;
If you don't like final comma you could remove it with:
WITH cte AS (
SELECT
IIF(Option1='true', 'Option 1,', '') +
IIF(Option2='true', 'Option 2,', '') +
IIF(Option3='true', 'Option 3,', '') AS Output
FROM table_name
WHERE TypeId = 2
)
SELECT LEFT(Output, len(Output)-1)
FROM cte
WHERE Output <> '';
EDIT:
How would I enclose this inside a CASE? Something like: CASE WHEN TypeId=1 THEN something ELSE (your statements)? When I try it I get a bunch of syntax errors.
Simply:
SELECT
CASE WHEN Option1='true' THEN 'Option 1,' ELSE '' END +
CASE WHEN Option2='true' THEN 'Option 2,' ELSE '' END +
CASE WHEN Option3='true' THEN 'Option 3,' ELSE '' END AS Output
FROM table_name
WHERE TypeId = 2;

If you want the options with no comma in the middle, then I think the easiest way is:
select stuff( ((case when option1 = 'true' then ', Option1' else '' end) +
(case when option2 = 'true' then ', Option2' else '' end) +
(case when option3 = 'true' then ', Option3' else '' end)
), 1, 2, ''
) as option_list
from t;
If you only want this for one of the types:
select (case when type = 1
then select stuff( ((case when option1 = 'true' then ', Option1' else '' end) +
(case when option2 = 'true' then ', Option2' else '' end) +
(case when option3 = 'true' then ', Option3' else '' end)
), 1, 2, ''
)
end) as option_list
from t;

Related

Concatenation by using CASE statement

I want to CONCAT between this on my SELECT statement in SQL Server.
All the columns are booleans.
The output will be like this if all of columns that are true
GGPNES
I tried to use this and it doesn't work
DECLARE #concat VARCHAR(40) ='';
SELECT
Smoke, Invoice,
Party, Summary,
MySelf, Export,
CASE
WHEN MyTable.Smoke = 1 OR MyTable.Invoice = 1
THEN #concat + 'GG'
WHEN MyTable.Party = 1
THEN #concat + 'P'
WHEN MyTable.Summary = 1
THEN #concat + 'N'
WHEN MyTable.MySelf = 1
THEN #concat + 'E'
WHEN MyTable.Export = 1
THEN #concat + 'S'
END
FROM
MyTable
I think that you want a concatenation of CASE expressions:
CASE WHEN Smoke = 1 OR Invoice = 1 THEN 'GG' ELSE '' END +
CASE WHEN Party = 1 THEN 'P' ELSE '' END +
CASE WHEN Summary = 1 THEN 'N' ELSE '' END +
CASE WHEN MySelf = 1 THEN 'E' ELSE '' END +
CASE WHEN Export = 1 THEN 'S' ELSE '' END

Separate concatenated Case When statements

I want to know if there is a way to separate this statement and create a sentence like One, Two - or Two, Three. The separator must not work unless there is something following.
case when d1.code = 'X' then 'One' else '' end +
case when d2.Code = 'Y' then 'Two' else '' end +
case when d3.code = 'Z' then 'Three' else '' end as PlzHelp
If you're on a recent version of SQL Server, use CONCAT_WS:
SELECT CONCAT_WS(',', CASE WHEN d1.code = 'X' THEN 'One' END,
CASE WHEN d2.Code = 'Y' THEN 'Two' END,
CASE WHEN d3.code = 'Z' THEN 'Three' END) AS PlzHelp
...
If you are on an older version, you could go for something like this
SELECT RTRIM(SUBSTRING(CASE WHEN d1.code = 'X' THEN ', One' ELSE '' END
+CASE WHEN d2.Code = 'Y' THEN ', Two' ELSE '' END
+CASE WHEN d3.code = 'Z' THEN ', Three' ELSE '' END
+' ' ,3 ,17)) AS PlzHelp

Display Column value according to need in sql

if i have a column in which ten digit values occurs like Column A = 11111000 so how to show this value in sql like (List,Add,Edit,Delete,Export) in sql.
there is a condition means if first position have 1 then it show List, If second Position have 1 it show Add, third position have 1 then Edit, fourth position have 1 then Delete, fifth position have 1 then Export.
If the value is a string, you can do:
select stuff( ((case when substring(a, 1, 1) = 1 then ',List' else '' end) +
(case when substring(a, 2, 1) = 1 then ',Add' else '' end) +
. . .
), 1, 1, '')
The logic is similar for bitwise operators:
select stuff( ((case when a & 2^9 then ',List' else '' end) +
(case when 2 & 2^8 then ',Add' else '' end) +
. . .
), 1, 1, '')
Maybe Substring function can help you to identify these values
select
id, ColumnA,
case when substring(ColumnA,1,1) = '1' then 'X' end as List,
case when substring(ColumnA,2,1) = '1' then 'X' end as [Add],
case when substring(ColumnA,3,1) = '1' then 'X' end as Edit,
case when substring(ColumnA,4,1) = '1' then 'X' end as [Delete],
case when substring(ColumnA,5,1) = '1' then 'X' end as [Export]
from Table1
I update the query according to comment
select
id, ColumnA,
stuff(
case when substring(ColumnA,1,1) = '1' then ',List' else '' end +
case when substring(ColumnA,2,1) = '1' then ',Add' else '' end +
case when substring(ColumnA,3,1) = '1' then ',Edit' else '' end +
case when substring(ColumnA,4,1) = '1' then ',Delete' else '' end +
case when substring(ColumnA,5,1) = '1' then ',Export' else '' end
,1,1,''
)
from Table1

how to show field name if it set to true

I have a table with the 'ContentSubCategory' name.its fields are like below:
Id int
FaName nvarchar(50)
IsSpecial bit
IsPerishable bit
IsDanger bit
now I Want a query that shows list of content names and the attributes that are set to True.
for example:
Row ContentInfo
------------------------------------
1 animal - IsSpecial
2 Human Body - IsSpecial,IsPerishable
3 Danger Goods - IsSpecial,IsDanger
how can I do that?
select cast(FaName as varchar)+' - '+cast(
case when IsSpecial = 1 then 'IsSpecial,' else '' as varchar)+cast(
case when IsPerishable = 1 then 'IsPerishable,' else '' as varchar)+cast(
case when IsDanger = 1 then 'IsDanger' else '' as varchar)
from your_table
Try this and adjust to your own like.
You can also use STUFF():
SELECT
FaName
,ISNULL(
NULLIF(
STUFF(
CASE WHEN IsSpecial = 1 THEN ', IsSpecial' ELSE '' END
+ CASE WHEN IsPerishable =1 THEN ', IsPerishable' ELSE '' END
+ CASE WHEN IsDanger = 1 THEN ', IsDanger' ELSE '' END
, 1, 2, '')
,'')
, 'Unknown')
As Class
FROM TableName

How to count non-null/non-blank values in SQL

I have data like the following:
And what I want is to count the PONo, PartNo, and TrinityID fields with a value in them, and output data like this:
How can I do this counting in SQL?
select
Job_number, Item_code,
case when RTRIM(PONo) = '' or PONo is null then 0 else 1 end +
case when RTRIM(PartNo) = '' or PartNo is null then 0 else 1 end +
case when RTRIM(TrinityID) = '' or TrinityID is null then 0 else 1 end
as [Count]
from YourTable
Try this:
select Job_Number = t.Job_Number ,
Item_Code = t.Item_Code ,
"Count" = sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
)
from dbo.my_table t
group by t.Job_Number , t.Item_Code
If you want to exclude data where all the tested fields are null or empty, add a having clause:
having sum( case ltrim(rtrim(coalesce( PONo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( PartNo , '' ))) when '' then 0 else 1 end
+ case ltrim(rtrim(coalesce( TrinityID , '' ))) when '' then 0 else 1 end
) > 0
Easy!