i have a string like \',,defoifcd,87765\' in a oracle table.Means, first two strings are empty.so i need to Split the string by comma as delimiter in Oracle.i am writing this qu
If you just have one string and you know it always has exactly four parts, you can split it like this, using only standard string functions (and avoiding regular expressions, which are more flexible but often slower).
NOTE: The second half of this answer addresses strings with variable number of "parts".
with inputs ( str ) as (
select ',,defoifcd,87765' from dual
)
-- end of TEST data; SQL query begins below (use your actual table and column names)
select substr(str, 1, instr(str, ',') - 1) as part_1,
substr(str, instr(str, ',') + 1,
instr(str, ',', 1, 2) - instr(str, ',') - 1) as part_2,
substr(str, instr(str, ',', 1, 2) + 1,
instr(str, ',', 1, 3) - instr(str, ',', 1, 2) - 1) as part_3,
substr(str, instr(str, ',', -1) + 1) as part_4
from inputs;
PART_1 PART_2 PART_3 PART_4
-------- -------- -------- --------
defoifcd 87765
1 row selected.
If the number of parts is not known in advance, it is better to get the output in a different format (see the output below). If one needs to arrange the parts in columns that can be done after all other processing is done - and it is always best left to the reporting application rather than done in SQL anyway.
with inputs ( id, str ) as (
select 1, ',,defoifcd,87765' from dual union all
select 2, '' from dual union all
select 3, 'a, b, c' from dual
)
-- end of TEST data; SQL query begins below (use your actual table and column names)
select id, str, level as part_number,
substr(aug_str, instr(aug_str, ',', 1, level) + 1,
instr(aug_str, ',', 1, level + 1) - instr(aug_str, ',', 1, level) - 1) as val
from ( select id, str, ',' || str || ',' as aug_str from inputs)
connect by level <= length(str) - length(translate(str, 'z,', 'z')) + 1
and prior id = id
and prior sys_guid() is not null
;
ID STR PART_NUMBER VAL
-- ---------------- ----------- ----------
1 ,,defoifcd,87765 1
1 ,,defoifcd,87765 2
1 ,,defoifcd,87765 3 defoifcd
1 ,,defoifcd,87765 4 87765
2 1
3 a, b, c 1 a
3 a, b, c 2 b
3 a, b, c 3 c
8 rows selected.