Solved

Split string function in Oracle

Posted on 2007-11-21
5
20,666 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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.

Join & Write a Comment

Suggested Solutions

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
Via a live example, show how to take different types of Oracle backups using RMAN.
This video shows how to recover a database from a user managed backup

771 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

11 Experts available now in Live!

Get 1:1 Help Now