Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

Hallo Jason,

thanks for Your hints, it's very clever solution.


It didn't help in my particular problem. I don't know how meny tokens are stored
in single field so I googled further and found this on ORACLE WEB-Site.
This example suits me better:
Tip of the Week
Tip for Week of July 9, 2007
Split a String
This tip comes from Brian Eye, a DBA, in Bristow, VA.
The following splits up the values returned from a string.
SQL> create table tab1 (owner number, cars varchar2(200));
Table created.
SQL> insert into tab1 (
select 1, 'Ford,Toyota,Nissan' from dual union all
select 2, 'Lexus,Mercedes,BMW,Infiniti' from dual union all
select 3, 'Ferrari' from dual union all
select 4, 'Porsche,Lotus,Lamborghini,Maserati,Aston Martin' from dual union all
select 5, 'Maybach,Bentley' from dual);
5 rows created.
SQL> col cars format a50
SQL> set pages 100
SQL> select * from tab1;
OWNER CARS
---------- --------------------------------------------------
1 Ford,Toyota,Nissan
2 Lexus,Mercedes,BMW,Infiniti
3 Ferrari
4 Porsche,Lotus,Lamborghini,Maserati,Aston Martin
5 Maybach,Bentley
SQL> col car format a20
SQL> break on owner
-- Oracle Database 10g version (using regular expressions)
SQL> select owner, car
from (
select owner
, regexp_substr(str, '[^,]+', 1, level) car
, level lv
, lag(level, 1, 0) over (partition by owner order by level) lg
from (
select owner
, ','||cars str
from tab1)
connect by regexp_substr(str, '[^,]+', 1, level) is not null)
where lv != lg;
OWNER CAR
---------- --------------------
1 Ford
Toyota
Nissan
2 Lexus
Mercedes
BMW
Infiniti
3 Ferrari
4 Porsche
Lotus
Lamborghini
Maserati
Aston Martin
5 Maybach
Bentley
15 rows selected.
--Oracle9i & Oracle Database 10g version
SQL> select owner, car
from (
select owner
, trim(substr(str, instr(str, ',', 1, level) + 1,
instr(str, ',', 1, level + 1) - instr(str, ',', 1, level) - 1)) car
, level lv
, lag(level, 1, 0) over (partition by owner order by level) lg
from (
select owner, ','||cars||',' str
from tab1)
connect by instr(str, ',', 1, level) > 0)
where car is not null
and lv != lg;
OWNER CAR
---------- --------------------
1 Ford
Toyota
Nissan
2 Lexus
Mercedes
BMW
Infiniti
3 Ferrari
4 Porsche
Lotus
Lamborghini
Maserati
Aston Martin
5 Maybach
Bentley
15 rows selected.
Here the original link
http://www.oracle.com/technology/oramag/code/tips2007/070907.html

You might also like