从Oracle中的字符串变量中提取多个数字

时间:2013-01-30 19:03:29

标签: sql regex string oracle oracle10g

我有一个varchar变量,其内容如下:'123,12,7654321,9998 ...'。 我在Oracle 10gR2中工作。 如何实现以下输出格式?:

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

谢谢!

3 个答案:

答案 0 :(得分:1)

试试这个

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

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

答案 1 :(得分:0)

以下是使用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;

我承认我从article改编了它。你要做的事情通常是放在一个叫做“拆分”的函数中。

答案 2 :(得分:0)

试试这个:

/* 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)