两个varchars之间的比较

时间:2018-07-24 07:18:22

标签: sql oracle

我需要您的帮助,以找到可以解决此问题的解决方案:

char1 : "1,2,3"
char2 : "1,2,3,4,5" 

并返回以下结果:

char3 : "4,5"

我该如何实现?

2 个答案:

答案 0 :(得分:0)

像这样吗?

SQL> with test (char1, char2) as
  2    (select '1,2,3', '1,2,3,4,5' from dual),
  3  ch1 as
  4    (select regexp_substr(char1, '[^,]+', 1, level) val
  5     from test
  6     connect by level <= regexp_count(char1, ',') + 1
  7    ),
  8  ch2 as
  9    (select regexp_substr(char2, '[^,]+', 1, level) val
 10     from test
 11     connect by level <= regexp_count(char2, ',') + 1
 12    )
 13  --
 14  select listagg(x.val, ',') within group (order by x.val) result
 15  from
 16  ( (select val from ch1
 17     minus
 18     select val from ch2
 19    )
 20    union
 21    (select val from ch2
 22     minus
 23     select val from ch1
 24    )
 25  ) x;

RESULT
-----------------------------------------------------------------------
4,5

SQL>

答案 1 :(得分:0)

还有另一个变体:

with t as 
( select 
 '1,2,3' as char1, 
 '1,2,3,4,5' as char2 
from dual )
select listagg(coalesce(a.parsed_char,b.parsed_char),',') within group (order 
by 1) as res
from (  select regexp_substr(char1, '[^,]+',1,level) as parsed_char 
    from t
    connect by regexp_substr(char1, '[^,]+',1,level) is not null ) a
full join 
 (  select regexp_substr(char2, '[^,]+',1,level) as parsed_char 
    from t
    connect by regexp_substr(char2, '[^,]+',1,level) is not null ) b
on (a.parsed_char = b.parsed_char)  
where a.parsed_char is null or b.parsed_char is null;