2017-05-30 696 views
0

如何在Oracle中使用SUBSTRINSTR拆分逗号分隔的字符串。如何在Oracle中拆分逗号分隔的字符串

String '20.4,12.5,3.5,0.2,0.2'

我尝试使用下面的代码,但我无法获得第二个逗号后的值。

SELECT substr('20.4,12.5,3.5,0.2,0.2',0,instr('20.4,12.5,3.5,0.2,0.2',',')-1) 
value FROM dual -- 1. 20.4 

第二个值我在第二个逗号后得到整个字符串。

SELECT substr('20.4,12.5,3.5,0.2,0.2',instr('20.4,12.5,3.5,0.2,0.2',',')+1,instr('20.4, 
12.5,3.5,0.2,0.2',',',2,2)-1) st FROM dual -- result : 12.5,3.5, 

我希望值的每个逗号后,像

20.4

12.5

3.5等。

+0

见https://stackoverflow.com/questions/26878291/split-string-by-delimiter-position-using-oracle-sql – realbart

+0

我希望它不使用“正规”的功能。 – Sam

+0

为什么你不想使用正则表达式函数? – realbart

回答

2

基于https://blogs.oracle.com/aramamoo/how-to-split-comma-separated-string-and-pass-to-in-clause-of-select-statement

首先,我们将形成一个查询,其将这个逗号分隔字符串,并给出了各个字符串作为行。

SQL> select regexp_substr('20.4,12.5,3.5,0.2,0.2','[^,]+', 1, level) from dual 
    connect by regexp_substr('20.4,12.5,3.5,0.2,0.2', '[^,]+', 1, level) is not null; 


REGEXP_SUBSTR('20.4,1 
--------------------- 
20.4     
12.5     
3.5     
0.2     
0.2 

通过逗号分隔的字符串上面的查询迭代,搜索逗号(,),然后通过处理逗号作为分隔符分割的字符串。它将字符串作为行返回,只要它碰到分隔符。