Oracle 12c Concatenate with brackets where nulls are involved - sql

We have a set of columns within a table we need to concatenate, and we need brackets around the third, fourth, fifth and sixth value, but also need nothing to appear if the column is null.
SELECT "ID",
NVL(PART || '.'|| SECTION ||'(' ||SUB1||')'|| '(' ||SUB2|| ')' || '('||SUB3||')' || '('||SUB4||')', '') as concatenated
FROM table1;
Places the values exactly right as long as there are values. When any one or more columns return null, we are getting an empty set of brackets for each null value.
Such as: 113.203()()()() when there are four null values
in this case we would need: 113.203
Or 113.450(h)(2)(iv)() when there is one null value.
here the desired results
would be 113.450(h)(2)(iv)
How can I change the script to leave out all the empty brackets when a null value is returned?
Thank you.

Hmmm, I think you want:
select id,
(part || '.' || section ||
(case when sub1 is not null then '(' || sub1 || ')' end) ||
(case when sub2 is not null then '(' || sub2 || ')' end) ||
(case when sub3 is not null then '(' || sub3 || ')' end) ||
(case when sub4 is not null then '(' || sub4 || ')' end)
) as concatenated
from table1;

Related

How to concatenate multiple columns where value may be be NULL

I need to pull a query from multiple columns from a table. Some rows will have data in one column, others will have two, three, and even four.
I tried to use this construct:
SELECT person_uid,('(' || major || NVL((',' ||second_major), '') || NVL((',' ||third_major), '') || NVL(',' ||fourth_major, '') || ')' ) AS MAJORS FROM academic_study
But the result would be like this:
6231 (BUMG,BUMK,,)
19091 (TDST,TDPG,,)
I need the parentheses, but not the trailing commas.
I could potentially strip out the extra commas in post processing, but I would prefer to do it in the SQL. I am using ORACLE.
You should fix your data model! Storing multiple columns with parallel data is awkward.
One method is:
select person_uid,
( '(' || major ||
(case when second_major is not null then ',' || second_major end) ||
(case when third_major is not null then ',' || third_major end) ||
(case when fourth_major is not null then ',' || fourth_major end) ||
')'
)

How can combine the values of multiple columns to a single comma separated column in Oracle SQL?

I need to create a column that has all the values from the previous columns combined and separated using commas ', '
I can't use listagg since I'm trying to combine multiple columns instead of rows.
below is an example of how the result column should look like, thanks.
Use string concatenation:
select trim(leading ',' from
(case when column1 is not null then ',' || column1 end) ||
(case when column2 is not null then ',' || column2 end) ||
(case when column3 is not null then ',' || column3 end)
)
This is also using a string concatenation but slightly different
SELECT
SUBSTR(
REPLACE(
', ' || NVL(column1,'!!') || ', ' || NVL(column2,'!!') || ', ' || NVL(column3,'!!')
,', !!','')
,3,100)

Combining a CASE WHEN statement & a column containing strings of type "Column 1 || '_' || Column 2" in Oracle SQL

I have a table consisting of three columns, which are called the following:
1) Month
2) Store_Type
3) City
I need this table to be expanded to contain five columns and the two columns that I wish to be added are detailed below.
Firstly, the query needs to create a new column called Store_Code. The Store_Code columns job is to store a numerical value which corresponds to what type of store it is.
I presume this would done using a CASE WHEN statement of the type:
SELECT Month,Store_Type,City,
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END Store_Code
FROM My_Table
After this is complete, I need to create a column known as "Store_Key". The values contained within the Store_Key column need to be of the following form:
"The Month For That Row""The Store Type For That Row""The City associated with that row"_"The Store Code for that row"
I imagine the best way to create this column would be to use a query similar to the following:
SELECT (My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
My_Table.Store_Code)
FROM My_Table
What I need is for these two separate queries to be combined into one query. I imagine this could be done by sub-setting the different SELECT queries but I am open to and grateful for any alternative solutions.
Thank you for taking the time to read through this problem and all solutions are greatly appreciated.
Do the case expression part inside a derived table (the subquery):
SELECT (My_Table2.Month || '_' || My_Table2.Store_Type || '_' || My_Table2.City || '_' ||
My_Table2.Store_Code)
FROM
(
SELECT Month,Store_Type,City,
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END Store_Code
FROM My_Table
) My_Table2
If this is you trying to populate your new columns, then you need an update statement. I would use two updates to ensure you get the store_case committed for your store_code. Otherwise if you're deriving it in real time, the subquery select answer would be the way to go.
update my_table
set store_case =
case store_type
when 'Corner Shop' then 1
when 'Megastore' THEN 2
when 'Petrol Station' THEN 3
...
else 10
end case;
commit;
update my_table
set store_code = Month || '_' || to_char(Store_Type) || '_' || City || '_' || Store_Code;
commit;
Why to use sub query? It can be done within single query as following:
SELECT My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
CASE
WHEN Store_Type = 'Corner Shop' THEN '1'
WHEN Store_Type = 'Megastore' THEN '2'
WHEN Store_Type = 'Petrol Station' THEN '3'
....
ELSE '10'
END as result
FROM My_Table
or you can use DECODE function as following:
SELECT My_Table.Month || '_' || My_Table.Store_Type || '_' || My_Table.City || '_' ||
DECODE(Store_Type,
'Corner Shop', '1',
'Megastore', '2',
'Petrol Station', '3'
....,
'10') -- this is default value same as else part of the case statement
as result
FROM My_Table
Cheers!!

Remove substring within aggregate

I am working in a data warehouse and combining 3 columns with the following:
CAST(
ISNULL(PORCH_TYPE_1,'') ||
CASE WHEN PORCH_TYPE_2 IS NULL THEN '' ELSE ', ' END ||
ISNULL(PORCH_TYPE_2,'') ||
CASE WHEN PORCH_TYPE_3 IS NULL THEN '' ELSE ', ' END ||
ISNULL(PORCH_TYPE_3,'') AS VARCHAR(250)
) AS PORCH_TYPE,
This is working, except in the results, I can end up with something that looks like:
Open Porch, None, None
or
None, None, Open Porch
What I'm needing to do is remove both
None
and
,None
How would I go about doing that within this same column/statement?
One method is to concatenate the comma to the end of each valid porch type. Then remove the final trailing comma using TRIM():
TRIM(TRAILING ',' FROM
((CASE WHEN PORCH_TYPE_1 <> 'None' THEN PORCH_TYPE_1 || ',' END) ||
(CASE WHEN PORCH_TYPE_2 <> 'None' THEN PORCH_TYPE_2 || ',' END) ||
(CASE WHEN PORCH_TYPE_3 <> 'None' THEN PORCH_TYPE_3 || ',' END)
)

ORA-00923 FROM keyword not found where expected

I am trying to concatenate some fields to return a single string for each row from an oracle table. This is in 10g. Here is my query:
SELECT t.value || '|' || t.label || '|' t.label_abbrv || '||' "mylist"
FROM list_value t
WHERE t.value BETWEEN 195001 AND 195300;
I'm getting the "FROM keyword not found where expected" error. This is really annoying. It's a simple query. I'm sure it's something simple I'm missing.
If you used SQLPLUS client, it would have saved you a little time:
SQL> SELECT value || '|' || label || '|' label_abbrv || '||' "mylist"
2 from list_value where (value between 195001 and 195300);
SELECT value || '|' || label || '|' label_abbrv || '||' "mylist"
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected
You can break up your query to multiple lines to isolate the problem:
SQL> edit
Wrote file afiedt.buf
1 SELECT value || '|'
2 || label ||
3 '|' label_abbrv ||
4 '||' "mylist"
5 from list_value
6 where
7* (value between 195001 and 195300)
SQL> /
'|' label_abbrv ||
*
ERROR at line 3:
ORA-00923: FROM keyword not found where expected
You might find SQLPLUS to be "primitive," but, hmmm, that's good for another question. Let me see if anyone else has asked about it yet.
D'oh! I found the problem. I'm missing a concat!
SELECT value || '|' || label || '|' ****||**** label_abbrv || '||' "mylist"
from list_value where (value between 195001 and 195300);
I think your answer to your own question is still wrong - it should be:
SELECT value || '|' || label || '|' || label_abbrv || '||' "mylist"
^^^^