Separate concatenated Case When statements - sql

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

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

SQL query with multiple/embedded CASE

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;

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

Multiple case statement not working as expected in Postgres

This is the query:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=1 THEN 'FT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=2 THEN 'ED' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=3 THEN 'MC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=4 THEN 'HC' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=5 THEN 'UV' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=6 THEN 'TT' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=7 THEN 'RX' ELSE '' END ||
CASE WHEN STRPOS(CAST(completed_phases::bit(8) AS VARCHAR),'1')=8 THEN 'PI' ELSE '' END
FROM rx_sales_order
If completed_phase is 129, my output for final column should be FTPI. But it is only showing FT. Only the first case statement seems to work, even if all of them are distinct.
STRPOS() will always return the first occurance of the searched string. So all calls to strpos() will return 1 for the input value 129.
You can use substring() instead:
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),1,1)='1' THEN 'FT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),2,1)='1' THEN 'ED' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),3,1)='1' THEN 'MC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),4,1)='1' THEN 'HC' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),5,1)='1' THEN 'UV' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),6,1)='1' THEN 'TT' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),7,1)='1' THEN 'RX' ELSE '' END ||
CASE WHEN substring(CAST(completed_phases::bit(8) AS VARCHAR),8,1)='1' THEN 'PI' ELSE '' END
Another option would be to use get_bit() to test each bit individually:
case when get_bit(completed_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(completed_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(completed_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(completed_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(completed_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(completed_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(completed_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(completed_phases::bit(8), 7) = 1 then 'PI' else '' END
A more flexible way of doing that is to turn the bits into rows and use an array as a lookup. Something like:
with lookup (codes) as (
values (array['FT','ED','MC','HC','UV','TT','RX','PI'])
)
SELECT completed_phases,
completed_phases::bit(8),
x.code
FROM rx_sales_order
join lateral (
select string_agg(codes[i],'') as code
from lookup, unnest(string_to_array(completed_phases::bit(8)::text, null)) with ordinality as t(b,i)
where b = '1'
) as x on true
The part regexp_split_to_table(completed_phases::bit(8)::text, '') with ordinality as t(b,i) will return the following for the value 129:
b | i
--+--
1 | 1
0 | 2
0 | 3
0 | 4
0 | 5
0 | 6
0 | 7
1 | 8
code[i] the uses the index to lookup the matching code and string_agg() then puts all selected codes together again into a single string. The condition where b = '1' only selects the bits that are set.
That solution will be substantially slower than the hardcoded case expression (because it increases the number of rows, just to reduce them again) - but it is more flexible and easier to maintain.
If you need that a lot, the best option would be to put the case expression into a function and use the function in your queries.
create or replace function get_codes(p_phases integer)
returns text
as
$$
select
case when get_bit(p_phases::bit(8), 0) = 1 then 'FT' else '' END||
case when get_bit(p_phases::bit(8), 1) = 1 then 'ED' else '' END||
case when get_bit(p_phases::bit(8), 2) = 1 then 'MC' else '' END||
case when get_bit(p_phases::bit(8), 3) = 1 then 'HC' else '' END||
case when get_bit(p_phases::bit(8), 4) = 1 then 'UV' else '' END||
case when get_bit(p_phases::bit(8), 5) = 1 then 'TT' else '' END||
case when get_bit(p_phases::bit(8), 6) = 1 then 'RX' else '' END||
case when get_bit(p_phases::bit(8), 7) = 1 then 'PI' else '' END
$$
language sql;
Then use:
SELECT DISTINCT
completed_phases,
get_codes(completed_phases) as codes
FROM rx_sales_order
As pointed out in the answer by a_horse_with_no_name, strpos will return the first occurrence of the searched string. At any rate, it's better to use get_bit instead of casting to VARCHAR to check if a bit is set. Also, instead of ||, you can use concat, which will handle nulls as blank strings. Your query could then be rewritten to:
SELECT DISTINCT
completed_phases,
CAST(completed_phases::bit(8) AS VARCHAR),
concat(CASE get_bit(completed_phases::bit(8), 0) WHEN 1 THEN 'FT' END,
CASE get_bit(completed_phases::bit(8), 1) WHEN 1 THEN 'ED' END,
CASE get_bit(completed_phases::bit(8), 2) WHEN 1 THEN 'MC' END,
CASE get_bit(completed_phases::bit(8), 3) WHEN 1 THEN 'HC' END,
CASE get_bit(completed_phases::bit(8), 4) WHEN 1 THEN 'UV' END,
CASE get_bit(completed_phases::bit(8), 5) WHEN 1 THEN 'TT' END,
CASE get_bit(completed_phases::bit(8), 6) WHEN 1 THEN 'RX' END,
CASE get_bit(completed_phases::bit(8), 7) WHEN 1 THEN 'PI' END)
FROM rx_sales_order;
On a side note, if you have the option to do so, I would recommend changing your database schema to store the phases as individual boolean columns instead of using a bit map. See Any disadvantages to bit flags in database columns? for a good discussion of why.

Combining SQL string alias into comma delimited string

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.