2010-12-10 68 views
2

類似的語句我需要寫一個sql語句是這樣的:使用解碼作爲Oracle

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

FROM mapchile.segmento 

但在這種情況下,我會得到一個「A」時id_segmento等於「1」,我需要即使當字符串id_Segmento包含'1'時,它也是'a',類似於類似的陳述。

還有其他像Decode這樣的命令是如何工作的?

謝謝。

回答

6

我會使用一個case語句。像

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

你的意思是''%1%''? – 2010-12-10 15:57:21

+0

是的。編輯。謝謝,傑克。 – 2010-12-13 14:05:01

4

的情況下使用運營商的綜合評估,而不是DECODE函數:

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

你的意思是' '%1%' '? – 2010-12-10 16:02:28

+0

@JackPDouglas:是的,thx – 2010-12-10 16:08:10

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')