如何在Oracle SQL中将定界字符串拆分为n(n-1)/ 2到组合?

时间:2018-11-17 10:48:40

标签: sql regex oracle oracle11g

例如“红色/绿色/蓝色” 分为以下

  

function counts = countn(A,n) if (A == 0) counts= 0; end if (n == mod(A,10)) disp(A); disp(floor(A/10)); disp(mod(A,10)); B = floor(A/10); counts = countn(B,n) + 1; else B = floor(A/10); countn(B,n); end end red red/green

red/green/blue

1 个答案:

答案 0 :(得分:2)

您可以将substrinst组合用作:

with t as
(
select 'red/green/blue' as str from dual
)
select substr(str,1,instr(str,'/',1,1)-1) str1,
       substr(str,1,instr(str,'/',1,2)-1) str2,
       str as str_whole
  from t;

STR1    STR2        STR_WHOLE
----    ----------  --------------
red     red/green   red/green/blue

如果您想逐行使用,则可以通过添加具有以下样式的regexp_count来使用:

with t as
(
select 'red/green/blue' as str from dual
)
select decode(sign(instr(str,'/',1,level)),1,substr(str,1,instr(str,'/',1,level)-1),str)
       as str
  from t
connect by level <= regexp_count(str,'/') + 1; 

STR 
--------------   
red     
red/green   
red/green/blue

Rextester Demo