我不認爲你需要儘可能的行固定擔心順序和字符串有SUBSTR值以相同的順序。
更新 OP要堅持XMLTABLE解決方案
使用XMLTABLE
SQL> WITH input_data AS
2 (SELECT 'number1,number2,number3' str1, 'word1, word2, word3' str2 FROM dual
3 ) ,
4 temp_data1 AS
5 (SELECT rownum rn,
6 trim(COLUMN_VALUE) str1
7 FROM input_data,
8 xmltable(('"'
9 || REPLACE(str1, ',', '","')
10 || '"'))
11 ) ,
12 temp_data2 AS
13 (SELECT rownum rn,
14 trim(COLUMN_VALUE) str2
15 FROM input_data,
16 xmltable(('"'
17 || REPLACE(str2, ',', '","')
18 || '"'))
19 )
20 SELECT t1.str1,
21 t2.str2
22 FROM temp_data1 t1,
23 temp_data2 t2
24 WHERE t1.rn = t2.rn
25/
STR1 STR2
------- -------
number1 word1
number2 word2
number3 word3
SQL>
使用REGEXP_SUBSTR
SQL> WITH DATA AS
2 (SELECT 'number1,number2,number3' str1, 'word1, word2, word3' str2 FROM dual
3 )
4 SELECT trim(regexp_substr(str1, '[^,]+', 1, LEVEL)) str1,
5 trim(regexp_substr(str2, '[^,]+', 1, LEVEL)) str2
6 FROM DATA
7 CONNECT BY LEVEL <= regexp_count(str1, ',')+1
8/
STR1 STR2
----------------------- -------------------
number1 word1
number2 word2
number3 word3
SQL>
更多的例子,請參閱
您在我的博客中找到的xmltable解決方案http://lalitkumarb.wordpress.com/2014/12/02/split-comma-delimited-string-into-rows-using-oracle-sql/將正常工作。你只需要加入他們。就像你加入兩張桌子一樣。 – 2015-04-06 06:20:12