Splitting the string into items
Assuming (for now, stay tuned) that Table1 has a single row, you can write a regular expression and use it in a recursive query to split the string in a single select.
level
denotes the level of recursion. regexp_substr
lets you specify the occurrence in the string. connect by
provides the exit condition: taking the next item, until there is none.
select
level as ItemNr,
regexp_substr(profile_value, '[^|]+', 1, level) as Item
from
Table1
connect by
regexp_substr(profile_value, '[^|]+', 1, level) is not null
Combining the wanted items into a new string
You could use ListAgg
with a case
condition, to combine only the items where level
is an even number back into a single string, with the string ' or '
as a separator.
select
listagg(
case when mod(level, 2) = 0 then
regexp_substr(profile_value, '[^|]+', 1, level)
end, ' or ') as Items
from
Table1
connect by
regexp_substr(profile_value, '[^|]+', 1, level) is not null
For multiple rows
The query above doesn't work well by itself if Table1 has multiple rows. It assumes a single input value. Using yet another trick, cross apply
, you can make this a subquery that does this transformation for each of the rows of your table.
The query below shows the original profile_value
column, and the transformed items
for each of the rows in Table1.
select
t.*, x.*
from
Table1 t
cross apply (
select
listagg(
case when mod(level, 2) = 0 then
regexp_substr(profile_value, '[^|]+', 1, level)
end, ' or ') as Items
from
dual -- just selecting a single profile_value
connect by
regexp_substr(profile_value, '[^|]+', 1, level) is not null
) x
In the end this works okay for a not to large set of data, but you're storying structured data into a string, and querying this is not very efficient.
If you need to repeat this query a lot, and the number of rows in Table1 is growing, it's probably better to store it in a more structured way.
If this data is coming from an external source, you could dump it in a temporary table, and transform it using the queries of this answer, to write it to a final, better structured table (or tables) for repeated use.