Regexp_like在case语句中无法正常工作(在Oracle PL / Sql中)

时间:2015-09-24 22:38:38

标签: plsql

我在Oracle中的以下案例陈述只给出了'其他'在所有记录的标志列中,但其他条件满足。

create table pca_title2 as
(select executive_id,title,
case
when    regexp_like (title,'^Sales$','i')   then    'Sales'
when    regexp_like (title,'^Ecommerce$','i')   then    'Ecommerce'
when    regexp_like (title,'^E-commerce$','i')  then    'E-commerce'
when    regexp_like (title,'^optimization$','i')    then    'optimization'
when    regexp_like (title,'^optimisation$','i')    then    'optimisation'
when    regexp_like (title,'^optimise$','i')    then    'optimise'
when    regexp_like (title,'^optimize$','i')    then    'optimize'
when    regexp_like (title,'^Online.Cust.experience$','i')  then    'Online.Cust.experience'
when    regexp_like (title,'^UX$','c')  then    'UX'
when    regexp_like (title,'^user Experience$','i') then    'user Experience'
when    regexp_like (title,'^operations$','i')  then    'operations'
when    regexp_like (title,'^GM$','c')  then    'GM'
when    regexp_like (title,'^Executive Director$','i')  then    'Executive Director'
when    regexp_like (title,'^Executive Chairman$','i')  then    'Executive Chairman'
when    regexp_like (title,'^Chief Officer$','i')   then    'Chief Officer'
when    regexp_like (title,'^General Manager$','i') then    'General Manager'
when    regexp_like (title,'^Chair$','i')   then    'Chair'
when    regexp_like (title,'^Advisor$','i') then    'Advisor'
when    regexp_like (title,'^Overseers$','i')   then    'Overseers'
when    regexp_like (title,'^Board Member $','i')   then    'Board Member '
when    regexp_like (title,'^Member of Board$','i') then    'Member of Board'
when    regexp_like (title,'^Chief Operating Officer$','i') then    'Chief Operating Officer'
when    regexp_like (title,'^COO$','c') then    'COO'
else 'others'
end as flag
from pca);

2 个答案:

答案 0 :(得分:1)

我会说正则表达式正如指定的那样工作。但是您没有提供任何数据。很可能你的正则表达式不是用来捕捉你想要的。看起来你正在寻找标题是那些(大多数不区分大小写)字符串中的任何一个的匹配。因此,它不会匹配任何包含之前或之后的文本的内容。

Chair - match
Chairs - no match
GM - match
GMC - no match

答案 1 :(得分:0)

您的正则表达式很可能正常工作,但不是您认为它应该工作的方式。您没有向我们展示任何数据,因此我们无法确定。

因为您使用锚点^$所有正则表达式只匹配完全字符串,并且因为您有i选项,所以匹配不区分大小写。这意味着您的数据中没有字符串SALES,但可能是SALES X

下面您将看到两个示例A和B.示例A是示例的简化版本。然而,它可能不会做你想要的,但希望它能让你知道你可能做错了什么。示例B向您展示了如何匹配(不区分大小写)具有子串sales的任何字符串。

with
x(id, title) as (
  select 1, 'Sales' from dual union all
  select 2, 'Ecommerce' from dual union all
  select 3, 'Online.Cust.experience' from dual union all
  select 4, 'user Experience' from dual union all
  select 5, 'Sales with postfix' from dual union all
  select 6, 'Something else' from dual
)
select 'A' as example, id, title
,case 
   when lower(title) in ('sales', 'ecommerce', 'online.cust.experience') then title
   else q'[It's something else]'
 end as flag
from x
union all
select 'B' as example, id, title
,case 
   when regexp_like (title, 'Sales', 'i') then 'Sales'
   else q'[It's something else]'
 end as flag
from x;