在Oracle中使用Decode作为类似语句

问题描述 投票:2回答:3

我需要写一个像这样的sql语句:

 SELECT id_segmento AS Segmento, Decode (id_segmento ,  '1' , 'a', 'b' )

 FROM mapchile.segmento

但在这种情况下,当id_segmento等于'1'时,我将获得'a',即使字符串id_Segmento包含'1',我也需要它为'a',类似于类似的法则。

还有像Decode这样的其他命令吗?

谢谢。

oracle decode sql-like
3个回答
7
投票

我会用一个案例陈述。就像是

case
   when id_segmento like '%1%' then 'a'
   else 'b'
end

5
投票

使用CASE运算符进行复杂评估,而不是使用DECODE函数:

SELECT id_segmento AS Segmento, 
       CASE
          WHEN id_segmento LIKE '%1%' THEN 
            'a'
          ELSE
            'b'
       END
  FROM mapchile.segmento

3
投票

如果你不想使用case,你仍然可以使用decodeinstr

decode(instr(id_segmento,'1'),0,'b','a')

我假设你想在场上的任何地方匹配'1'。如果你想匹配以'1'开头的字段,那么你可以使用:

decode(ascii(id_segmento),49,'a','b')

要么

decode(substring(id_segmento,1,1),'1','a','b')

要么

decode(instr(id_segmento,'1'),1,'a','b')
© www.soinside.com 2019 - 2024. All rights reserved.