我有两列,说值列有像'abc|comp|science|raja'
这样的记录,我想像abc,comp,science,raja
那样分割这些记录,我需要将它与另一列说成CHECKER
进行比较已有科学记录
abc|comp|science|raja
science
答案 0 :(得分:2)
您不需要分割字符串,甚至不需要正则表达式;只需检查checker
字符串(带后跟定界符)是否是value
的子字符串(带前导和尾随定界符):
Oracle设置:
CREATE TABLE your_table ( value, checker ) as
SELECT 'abc|comp|science|raja', 'science' FROM DUAL UNION ALL
SELECT 'abc|def|ghi', 'xyz' FROM DUAL UNION ALL
SELECT 'abc', 'abc' FROM DUAL UNION ALL
SELECT 'abcdef', 'abc' FROM DUAL
查询:
SELECT *
FROM your_table
WHERE '|' || value || '|' LIKE '%|' || checker || '|%';
输出:
VALUE | CHECKER :-------------------- | :------ abc|comp|science|raja | science abc | abc
db <>提琴here
答案 1 :(得分:1)
您可以将管道分隔的字符串拆分为各个值,并将其表示为表格。然后,您可以加入Checkers表。 网络上到处都是示例,有两种实现方法。
WITH test_tab AS
(SELECT 'abc|comp|science|raja' str FROM dual
)
SELECT regexp_substr (str, '[^|]+', 1, rownum) split
FROM test_tab
CONNECT BY LEVEL <= LENGTH (regexp_replace (str, '[^|]+')) + 1;
SELECT
column_value
FROM
TABLE(apex_string.split(
'abc|comp|science|raja',
'|'
));
答案 2 :(得分:0)
这里是一个选择:
SQL> with test (id, value, checkers) as
2 (select 1, 'abc|comp|science|raja', 'science' from dual union all
3 select 2, 'xyz|bla|nothing' , 'zzz' from dual
4 )
5 select t.id,
6 regexp_substr(t.value, '[^\|]+', 1, column_value) val,
7 column_value rn,
8 t.checkers,
9 --
10 case when regexp_substr(t.value, '[^\|]+', 1, column_value) = t.checkers then 'Match'
11 else 'No match'
12 end what
13 from test t cross join table(cast(multiset(select level from dual
14 connect by level <= regexp_count(t.value, '\|') + 1
15 ) as sys.odcinumberlist))
16 order by t.id, rn;
ID VAL RN CHECKER WHAT
---------- --------------------- ---------- ------- --------
1 abc 1 science No match
1 comp 2 science No match
1 science 3 science Match
1 raja 4 science No match
2 xyz 1 zzz No match
2 bla 2 zzz No match
2 nothing 3 zzz No match
7 rows selected.
SQL>
答案 3 :(得分:0)
听起来您只想检查检查器是否在值中,在这种情况下,您可以执行以下操作:
with mytable as
( select 'abc|comp|science|raja' value
, 'science' as checker
from dual
union all
select 'science|abc|comp|raja'
, 'science'
from dual
union all
select 'abc|comp|raja|science'
, 'science'
from dual )
select x.value
from mytable x
where regexp_like(value, '(^|\|)' || checker || '($|\|)')
regex_like正在使用管道或字符串结尾在检查程序的值中进行搜索,因此它将与字符串的开头,中间或结尾处的关键字匹配。但这与“科学”不符。
或者,如果您想查看所有行并检查它们是否通过了“检查”,则可以执行以下操作:
with mytable as
( select 'abc|comp|science|raja' value
, 'science' as checker
from dual
union all
select 'science|abc|comp|raja'
, 'science'
from dual
union all
select 'abc|comp|raja|science'
, 'science'
from dual
union all
select 'abc|comp|raja|sciences'
, 'science'
from dual )
select x.value
, x.checker
, case when regexp_substr(value, '(^|\|)' || checker || '($|\|)') is not null
then 'Y'
end as passed
from mytable x