您好,登錄后才能下訂單哦!
流程控制函數(shù) DECODE
decode()函數(shù)簡(jiǎn)介:
主要作用:
將查詢結(jié)果翻譯成其他值(即以其他形式表現(xiàn)出來,以下舉例說明);
使用方法:
Select decode(columnname,值1,翻譯值1,值2,翻譯值2,…值n,翻譯值n,缺省值)
From talbename
Where …
其中columnname為要選擇的table中所定義的column,
含義解釋:
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
注:其中缺省值可以是你要選擇的column name 本身,也可以是你想定義的其他值,比如Other等;
舉例說明:
現(xiàn)定義一table名為output,其中定義兩個(gè)column分別為monthid(var型)和sale(number型),若sale值=1000時(shí)翻譯為D,=2000時(shí)翻譯為C,=3000時(shí)翻譯為B,=4000時(shí)翻譯為A,如是其他值則翻譯為Other;
SQL如下:
Select monthid,decode(sale,1000,'D',2000,'C',3000,'B',4000,'A',’Other’) sale from output
特殊情況:
若只與一個(gè)值進(jìn)行比較
Select monthid ,decode(sale, NULL,‘---’,sale) sale from output
另:decode中可使用其他函數(shù),如nvl函數(shù)或sign()函數(shù)等;
比較大小函數(shù) sign
函數(shù)語法:
sign(n)
函數(shù)說明:
取數(shù)字n的符號(hào),大于0返回1,小于0返回-1,等于0返回0
示例:
1、select sign( 100 ),sign(- 100 ),sign( 0 ) from dual;
SIGN(100) SIGN(-100) SIGN(0)
———- ———- ———-
1 -1 0
2、a=10,b=20
則sign(a-b)返回-1
NVL(EXPR1,EXPR2)
若EXPR1是NULL,則返回EXPR2,否則返回EXPR1.
SELECT NAME,NVL(TO_CHAR(COMM),'NOT APPLICATION') FROM TABLE1;
如果用到decode函數(shù)中就是
select monthid,decode(nvl(sale,6000),6000,'NG','OK') from output
sign()函數(shù)根據(jù)某個(gè)值是0、正數(shù)還是負(fù)數(shù),分別返回0、1、-1,
如果取較小值就是
select monthid,decode(sign(sale-6000),-1,sale,6000) from output即達(dá)到取較小值的目的。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。