sql如何防止like语句掩蔽

u7up0aaq  于 2021-06-24  发布在  Hive
关注(0)|答案(2)|浏览(234)

我在处理一个掩盖问题 like 声明如下:

case
   when name like 'PO_UTI_%' then 'UTI' 
   when name like 'PO_UTI_ADR_%' then 'UTI ADDRESS'
   when name like 'PO_OP_%' then 'OP' 
   when name like 'PO_OP_HH_%' then 'OP HH' 
   end newname

新名称uti将屏蔽uti地址和op将屏蔽op hh,我应该如何避免这个屏蔽问题?
谢谢您!

jv4diomz

jv4diomz1#

按这个顺序写这个案子。

case
   when name like 'PO_UTI_ADR_%' then 'UTI ADDRESS'
   when name like 'PO_UTI_%' then 'UTI'
   when name like 'PO_OP_HH_%' then 'OP HH'  
   when name like 'PO_OP_%' then 'OP' 
   end newname
hgtggwj0

hgtggwj02#

你把条件安排得更好:

(case when name like 'PO_UTI_ADR_%' then 'UTI ADDRESS'
      when name like 'PO_UTI_%' then 'UTI' 
      when name like 'PO_OP_HH_%' then 'OP HH' 
      when name like 'PO_OP_%' then 'OP' 
 end) as newname

实际上,最好按长度排序:

(case when name like 'PO_UTI_ADR_%' then 'UTI ADDRESS'
      when name like 'PO_OP_HH_%' then 'OP HH' 
      when name like 'PO_UTI_%' then 'UTI' 
      when name like 'PO_OP_%' then 'OP' 
 end) as newname

这将帮助您确保没有“掩蔽”冲突。

相关问题