0

I have a varchar variable with contents like this: '123,12,7654321,9998...'. I work in Oracle 10gR2. How can I achieve the following output format?:

------
123
12
7654321
9998
...
------

Thanks!

0

3 Answers 3

1

Try this

SELECT EXTRACTVALUE(COLUMN_VALUE,'text()') VALS 
FROM XMLTABLE('123,12,7654321,9998');

or

SELECT COLUMN_VALUE VALS FROM XMLTABLE('123,12,7654321,9998');
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a way using connect:

with str as (select '123,12,7654321,9998' as val from dual)
select cast(regexp_substr(val,'[^,]+', 1, level) as int)
from str
connect by cast(regexp_substr(val, '[^,]+', 1, level) as int) is not null;

I'll admit that I adapted it from this article. What you are trying to do is commonly put into a function called "split".

1 Comment

Hm, thank you, Gordon Linoff! I'll try to apply your advice..Unfortunately, I don't know about connect by cast...It's time for learning!)
0

Try this :

/* Formatted on 1/30/2013 1:24:07 PM (QP5 v5.227.12220.39724) */
WITH val AS (SELECT '123,12,7654321,9998' txt FROM DUAL)
SELECT REGEXP_SUBSTR (txt,
              '[0-9]+|[a-z]+|[A-Z]+',
              1,
              lvl)
  FROM (SELECT txt,
           LEVEL lvl
      FROM val
    CONNECT BY LEVEL <=   LENGTH (txt)
                - LENGTH (REPLACE (txt,
                           ','))
                + 1)

2 Comments

Thank you, Swapna Mohan! It works too!) Can you advise any books about regular expression or all I need is reading docs.oracle.com?
I am sorry, I don't know of any books on regular expressions. docs.oracle.com is all i used for that and ofcourse code from projects that I worked on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.