Just for fun - here is a way to get the manufacturer and the model in separate columns, without the \n
, the commas and the double-quotes. You may adapt the solution to get you the manufacturer and model, enclosed in double-quotes and separated by comma, in a single column, if indeed that is the requirement. (If that was meant as an intermediate step, you don't need it.) Note that I prefer to work with the double-quote character as a "marker" to avoid having to deal with \n
- and you can do so, too, if you need to modify the solution below.
I assumed the strings are exactly in the format you show. I created a small test table with two rows to show how the solution works in that case; when there is a single row you may not run into some of the complications present when there are multiple rows, and I wanted a more general solution.
I wrote the solution using only standard string functions (no regular expressions), which should result in faster execution.
with
test_data ( id, str ) as (
select 1, '"Honda","Civic"\n"Toyota","Camry"\n"Ford","Fusion"' from dual union all
select 2, '"Chevrolet","Volt"\n"Tesla","Model S"' from dual
)
-- end of test data; solution (SQL query) begins below this line
select id,
substr(str, 1 + instr(str, '"', 1, 1 + 4 * (level - 1)),
instr(str, '"', 1, 2 + 4 * (level - 1)) -
instr(str, '"', 1, 1 + 4 * (level - 1)) - 1) as manufacturer,
substr(str, 1 + instr(str, '"', 1, 3 + 4 * (level - 1)),
instr(str, '"', 1, 4 + 4 * (level - 1)) -
instr(str, '"', 1, 3 + 4 * (level - 1)) - 1) as model_name
from test_data
connect by level <= 1 + length(str) - length(replace(str, '\', ''))
and prior id = id
and prior sys_guid() is not null
;
ID MANUFACTURER MODEL_NAME
--- ------------ ------------
1 Honda Civic
1 Toyota Camry
1 Ford Fusion
2 Chevrolet Volt
2 Tesla Model S
5 rows selected.