Solved

Split string function in Oracle

Posted on 2007-11-21
5
20,680 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
  • 2
  • 2
5 Comments
 
LVL 73

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 73

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now