You could do it in pure SQL using SUBSTR and INSTR to extract the substrings between the delimiter |
.
For simple string manipulation, I prefer SUBSTR + INSTR
approach as it is still faster than the REGEXP. Of course with recent releases, the performance difference is decreasing, however, REGEXP is still more CPU intensive than the old SUBTR+INSTR
.
For example,
SQL> WITH sample_data AS
2 (SELECT 'AP1|Apple Juice|100' str
3 FROM dual
4 )
5 -- end of sample_data mocking as a table
6 SELECT str,
7 SUBSTR(str, 1, instr(str, '|', 1,1) -1) str1,
8 SUBSTR(str, instr(str, '|', 1, 1) + 1,
9 instr(str, '|', 1, 2) -
10 instr(str, '|', 1, 1) -1) str2,
11 SUBSTR(str, instr(str, '|', 1, 2) + 1) str3
12 FROM sample_data;
STR STR1 STR2 STR3
------------------- ------------------- ------------------- ----
AP1|Apple Juice|100 AP1 Apple Juice 100
On a side note, you should not store data as delimited string, it is really a bad design. You should normalize it as a permanent fix. As far as possible, store the attributes in different columns, then you do not have the overhead of manipulating the string to extract respective attributes.