oracle函式decode用法

2021-08-30 19:54:14 字數 1170 閱讀 2244

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

return(預設值)

end if

eg:select decode('&n','1','hello','2','world') from dual;當你n傳入引數為1的時候會輸出hello,n=2時輸出world。

2.decode(表示式a,b,c,d)

這個的意思是當表示式a的值等於『b的時候顯示『c』否則顯示『d』

eg:select decode(sum(&a+&b),10,'the sum is 10','the sum is not 10') from dual;當你的輸入a+b=10的時候輸出'the sum is 10'。否則輸出the sum is not 10

decode和case when .... then  else ....end比較

eg:select e.* ,case when e.deptno = 20 then

'e.sal >= 1500 '

else

'e.sal>2500'

end as sals from emp e;

decode用的比較多的地方就是取值判斷。例如:user表的status欄位為0時顯示禁用,為1是顯示可用。直接用decode函式取值,到前台顯示的時候就不用再if esle的判斷了。

順便提一下to_date和to_char的區別

to_date(a,'yyyy-mm-dd')是將字串轉換為資料庫的日期型別

to_char(a,'yyyy-mm-dd')是將日期或數字轉換成字串

eg:使用to_char函式處理數字

to_char(number, '格式')

to_char(salary,』$99,999.99』);

使用to_char函式處理日期

to_char(date,』格式』);

Oracle函式 DECODE 函式

decode 函式,它將輸入數值與函式中的引數列表相比較,根據輸入值返回乙個對應值。函式的引數列表是由若干數值及其對應結果值組成的若干序偶形式。當然,如果未能與任何乙個實參序偶匹配成功,則函式也有預設的返回值。區別於sql的其它函式,decode函式還能識別和操作空值。語法 decode contr...

oracle常用函式 decode

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

oracle常用函式 decode

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