This document provides an example of how to split a string containing comma-separated values into individual rows in Oracle. It shows SQL queries that use the REGEXP_SUBSTR and INSTR functions to split the string stored in a single column into multiple rows, with one value per row. Testing it on a table with sample data containing owners and a column storing multiple car names as a comma-separated string, the queries are able to successfully split the strings and display the results with one car name per row while retaining the associated owner.
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
73 views
Split A String
This document provides an example of how to split a string containing comma-separated values into individual rows in Oracle. It shows SQL queries that use the REGEXP_SUBSTR and INSTR functions to split the string stored in a single column into multiple rows, with one value per row. Testing it on a table with sample data containing owners and a column storing multiple car names as a comma-separated string, the queries are able to successfully split the strings and display the results with one car name per row while retaining the associated owner.
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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