Solved

Split string function in Oracle

Posted on 2007-11-21
5
20,698 Views
Last Modified: 2013-12-18
Hi,
can someone help me write a function to split a string separated by '/' into separate values, please? (Oracle 10g)
say, I have 'one/two/three/four' and need
1
2
3
4
Thanks!
0
Comment
Question by:pshik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 20329588
Here's a function to split the string on any delimiter
it's a slight variation from Tom Kyte's function found on askTom



CREATE OR REPLACE FUNCTION str2tbl(p_string IN VARCHAR2, p_delimiter IN VARCHAR2 := ',')
    RETURN vcarray PIPELINED
AS
    v_length   NUMBER := LENGTH(p_string);
    v_start    NUMBER := 1;
    v_index    NUMBER;
BEGIN
    WHILE(v_start <= v_length)
    LOOP
        v_index    := INSTR(p_string, p_delimiter, v_start);

        IF v_index = 0
        THEN
            PIPE ROW(SUBSTR(p_string, v_start));
            v_start    := v_length + 1;
        ELSE
            PIPE ROW(SUBSTR(p_string, v_start, v_index - v_start));
            v_start    := v_index + 1;
        END IF;
    END LOOP;

    RETURN;
END str2tbl;


select * from table(str2tbl('one/two/three/four','/'))



as for converting from one to 1, two to 2,  you'll have to write your own number translator
0
 
LVL 27

Accepted Solution

by:
sujith80 earned 125 total points
ID: 20332366
Use the following query:

WITH Y as (select  'one/two/three/four' val from dual)
select substr(val, (decode(level, 1, 0 , instr(val,'/',1, level-1) ) + 1) ,
(decode( instr(val,'/',1, level) -1 , -1, length(val), instr(val,'/',1, level) -1 )) - (decode(level, 1, 0 , instr(val,'/',1, level-1) ) + 1) + 1) a
from Y connect by level <= (select (length(val) - length(replace(val , '/', null)) ) from Y ) + 1
/
0
 

Author Comment

by:pshik
ID: 20335564
to sdstuber:
yes, I saw that example when I googled for it. Unfortunately, it doesn't work for me. Thanks though:)
Regarding converting, I don't need it, it was a typo, sorry.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 20335648
doesn't work?  what does it not do?

if you won't want the data returned in rows, what are you trying to do?
0
 

Author Comment

by:pshik
ID: 20343068
sdstuber,
it's a school project and we haven't covered pipeline so i can't use it. Besides, it gives me error messages. Thanks anyway:)

I am going to go with the query. Thanks sujith80!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question