Oracle函式 DECODE 函式

2021-08-30 20:51:19 字數 1233 閱讀 2550

decode()函式,它將輸入數值與函式中的引數列表相比較,根據輸入值返回乙個對應值。函式的引數列表是由若干數值及其對應結果值組成的若干序偶形式。當然,如果未能與任何乙個實參序偶匹配成功,則函式也有預設的返回值。 

區別於sql的其它函式,decode函式還能識別和操作空值。

語法:decode(control_value,value1,result1[,value2,result2…][,default_result]);

control _value試圖處理的數值。decode函式將該數值與後面的一系列的偶序相比較,以決定返回值。

value1是一組成序偶的數值。如果control _value返回值與之匹配成功,則相應的結果result1將被返回,反之就與下個序偶相比較.

如果對應乙個空的返回值,可以使用關鍵字null於之對應

result1 是一組成序偶的結果值。

default_result 未能與任何乙個值匹配時,函式返回的預設值。

例如:

selectdecode( x , 1 , 『x is 1 』, 2 , 『x is 2 』, 『others』) from dual

當x等於1時,則返回『x is 1』。

當x等於2時,則返回『x is 2』。

否則,返回others』。

需要,比較2個值的時候,可以配合sign()函式一起使用。

select decode( sign(5 -6), 1 'is positive', -1, 'is nagative', 'is zero')

同樣,也可以用case實現:

select case sign(5 - 6)

when 1 then 'is positive'

when -1 then 'is nagative'

else 'is zero' end

from dual

在oracle中利用sign函式實現if-elseif-end的功能 ,下面是decode和sign的組合使用

select 欄位i,decode(sign(欄位i-90+1), 1,'a',

decode(sign(欄位i-75+1), 1,'b',

decode(sign(欄位i-60+1),1,'c','d')

) ) grade_char

from table

order by grade_char,欄位i;

oracle最強大函式之一decode函式的使用

decode的幾種用法 1 使用decode判斷字串是否一樣 decode value,if1,then1,if2,then2,if3,then3,else 含義為if 條件 值1 then return value 1 elsif 條件 值2 then return value 2 elsif 條...

oracle函式decode用法

decode用法 1。decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 返回值1 elsif 條件 值2 then return 返回值2 elsif 條件 值n then return 返回值n else ret...

oracle常用函式 decode

decode函式在實際開發中非常的有用,而且功能比較強大,與其他函式結合,能讓很多的工作變得簡單 decode 字段,條件1,結果值1,條件2,結果值2,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif...