简体   繁体   中英

Extracting multiple numbers from string variable in Oracle

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!

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');

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".

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM