decode函式用法

2021-05-22 21:12:55 字數 1326 閱讀 7262

decode

函式將該數值與一系列的序偶相比較,以決定最後的返回結果

。序偶序偶,就是成對出現

;我們將這個函式與

switch

比較,我們發現他們基本相似,唯一不同的是

switch

將要比較的值放在

case

後面都加上了

break中。

select id,

decode(flag,』y』,'yes』,'n』,』no』,null,』none』,'yes』)

from t_test;

t_testflag欄位為y時返回yes,為n時返回no,為null時返回none,預設返回yes

看看我遇到的問題:

select a.id,

a.user_name,

a.oper_time time2,

a.oper_type,

a.description,

a.oper_flag,

decode(a.oper_flag, '1', '

成功', '

失敗') oper_flag,

a.ip,

b.true_name

from sec_log_operation a

left join sec_user b on a.user_name = b.user_name

where 1 = 1

order by oper_time desc

原來這紅色部分是:

decode(a.oper_flag, 1, '

成功', '

失敗')

在查詢的時候會報錯,原因是

oper_flag

這個字段資料中有非數字型別,也就是這個字段不僅有1、

0,還有「失敗

」,這樣資料不等於字元。

所以把1

當字元處理就可以了。

Decode函式用法

decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif 條件 值n then return 翻譯值n else return 預設值 end...

decode函式的用法

含 釋 decode 條件,值1,翻譯值1,值2,翻譯值2,值n,翻譯值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 elsif 條件 值n then return 翻譯值n else return 預設值...

decode函式的用法

decode的作用當字段或字段的運算的值等於值1時,該函式返回值2,否則返回值3 含 釋 decode 條件,值1,返回值1,值2,返回值2,值n,返回值n,預設值 該函式的含義如下 if 條件 值1 then return 翻譯值1 elsif 條件 值2 then return 翻譯值2 els...