oracle 在oracle中使用Decode作为like语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4410066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 02:57:37  来源:igfitidea点击:

Using Decode as a like statement in oracle

oracledecodesql-like

提问by Giancarlo Solarino

I need to write a sql statement like this:

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

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

 FROM mapchile.segmento

but in this case I will obtain an 'a' when id_segmento is equal to '1', I need it to be 'a' even when the string id_Segmento contains the '1', kind of like and like statment.

但在这种情况下,当 id_segmento 等于 '1' 时,我将获得一个 'a',即使字符串 id_Segmento 包含 '1',我需要它是 'a',有点像和喜欢语句。

There is any other command like Decode that works this way?

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

Thanks.

谢谢。

回答by Jim Hudson

I'd use a case statement. Something like

我会使用 case 语句。就像是

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

回答by Vincent Malgrat

Use the CASE operator for complex evaluation instead of the DECODE function:

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

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

回答by Vincent Malgrat

if you don't want to use caseyou can still use decodeand instr:

如果您不想使用,case您仍然可以使用decodeand instr

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

I'm assuming you want to match on a '1' anywhere in the field. If you want to match on fields that start with a '1' then you could use:

我假设您想在现场的任何地方匹配“1”。如果要匹配以“1”开头的字段,则可以使用:

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

or

或者

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

or

或者

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