我需要写一个像这样的sql语句:
SELECT id_segmento AS Segmento, Decode (id_segmento , '1' , 'a', 'b' )
FROM mapchile.segmento
但在这种情况下,当id_segmento等于'1'时,我将获得'a',即使字符串id_Segmento包含'1',我也需要它为'a',类似于类似的法则。
还有像Decode这样的其他命令吗?
谢谢。
我会用一个案例陈述。就像是
case
when id_segmento like '%1%' then 'a'
else 'b'
end
使用CASE运算符进行复杂评估,而不是使用DECODE函数:
SELECT id_segmento AS Segmento,
CASE
WHEN id_segmento LIKE '%1%' THEN
'a'
ELSE
'b'
END
FROM mapchile.segmento
如果你不想使用case
,你仍然可以使用decode
和instr
:
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')