我有一个 varchar 变量,其内容如下:'123,12,7654321,9998...'。我在 Oracle 10gR2 工作。如何实现以下输出格式?:
------
123
12
7654321
9998
...
------
谢谢!
试试这个
SELECT EXTRACTVALUE(COLUMN_VALUE,'text()') VALS
FROM XMLTABLE('123,12,7654321,9998');
或者
SELECT COLUMN_VALUE VALS FROM XMLTABLE('123,12,7654321,9998');
这是一种使用方法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;
我承认我改编自这篇文章。您要做的通常是放入一个名为“split”的函数中。
试试这个 :
/* 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)