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
Related
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
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;
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
I am working on this stored proc which check to see which BIT fields are true and depending on it assigns a text to it using alias name. I want to be able to combine these alias values (string) and have it comma delimited to I can display it on my SSRS report.
Below is part of my stored proc.
,CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN 'Reservation Already Sent'
END AS ReservationAttached
,CASE
WHEN sr.[HPOfficeAttached] = 1 THEN 'H&P Office'
END AS HPOfficeAttached
WHEN sr.[NotesAttached] = 1 THEN 'Notes'
END AS NotesAttached
,CASE
WHEN sr.[OpPermitAttached] = 1 THEN 'Op Permit'
END AS OpPermitAttached
,CASE
WHEN sr.[TestResultsAttached] = 1 THEN 'Test Results'
END AS TestResultsAttached
,CASE
WHEN sr.[ConsultationReportsAttached] = 1 THEN 'Consultation Reports'
END AS ConsultationReportsAttached
,CASE
WHEN sr.[OtherAttached] = 1 THEN 'Other'
END AS OtherAttached
so lets say in case where only NotesAttached and ReservationAlreadySentAttached were only true ones then I want the end result to come out as Notes, Reservation Already Sent.
How do i concatenate these aliases ?
Another option is to build your string and then just remove the leading comma with STUFF()
Example (abbreviated)
Declare #YourTable table (ReservationAlreadySentAttached bit,HPOfficeAttached bit,NotesAttached bit)
Insert Into #YourTable values
(1,0,1)
Select NewValue = stuff(
case when ReservationAlreadySentAttached = 1 then ', Reservation Already Sent' else '' end
+case when HPOfficeAttached = 1 then ', H&P Office' else '' end
+case when NotesAttached = 1 then ', Notes' else '' end
,1,2,'')
From #YourTable
Returns
NewValue
Reservation Already Sent, Notes
EDIT - Just the Expression
NewValue = stuff(
case when sr.[ReservationAlreadySentAttached] = 1 then ', Reservation Already Sent' else '' end
+case when sr.[HPOfficeAttached] = 1 then ', H&P Office' else '' end
+case when sr.[NotesAttached] = 1 then ', Notes' else '' end
,1,2,'')
Not the prettiest solution...
RIGHT((CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END)
,LEN(CASE
WHEN sr.[ReservationAlreadySentAttached] = 1
THEN ',Reservation Already Sent'
END + CASE
WHEN sr.[HPOfficeAttached] = 1
THEN ',H&P Office'
END + CASE
WHEN sr.[NotesAttached] = 1
THEN ',Notes'
END + CASE
WHEN sr.[OpPermitAttached] = 1
THEN ',Op Permit'
END + CASE
WHEN sr.[TestResultsAttached] = 1
THEN ',Test Results'
END + CASE
WHEN sr.[ConsultationReportsAttached] = 1
THEN ',Consultation Reports'
END + CASE
WHEN sr.[OtherAttached] = 1
THEN ',Other'
END) - 1 )
Edit: You may have to take into consideration when NONE of them are 1 so you don't get Invalid length parameter passed to the right function.
CASE
WHEN sr.[ReservationAlreadySentAttached] = 1 THEN ', Reservation Already Sent' ELSE ''
END
+ CASE
WHEN sr.[HPOfficeAttached] = 1 THEN ', H&P Office' ELSE ''
END
+ CASE etc...
And wrap this in some function to remove the first comma + space.
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!