oracle decode函式使用方法

來源:酷知科普網 2.53W

decode()函式是ORACLE PL/SQL是功能強大的函式之一,目前還只有ORACLE公司的SQL提供了此函式,其他資料庫廠商的SQL實現還沒有此功能。

操作方法

(01)DECODE函式是ORACLE PL/SQL是功能強大的函式之一,目前還只有ORACLE公司的SQL提供了此函式,其他資料庫廠商的SQL實現還沒有此功能。DECODE有什麼用途 呢? 先構造一個例子,假設我們想給智星職員加工資,其標準是:工資在8000元以下的將加20%;工資在8000元以上的加15%,通常的做法是,先選出記錄 中的工資欄位值? select salary into var-salary from employee,然後對變數var-salary用if-then-else或choose case之類的流控制語句進行判斷。 如果用DECODE函式,那麼我們就可以把這些流控制語句省略,通過SQL語句就可以直接完成。如下:select decode(sign(salary - 8000),1,salary*1.15,-1,salary*1.2,salary from employee 是不是很簡潔?

(02)DECODE的語法:DECODE(value,if1,then1,if2,then2,if3,then3,...,else),表示如果value 等於if1時,DECODE函式的結果返回then1,...,如果不等於任何一個if值,則返回else。初看一下,DECODE 只能做等於測試,但剛才也看到了,我們通過一些函式或計算替代value,是可以使DECODE函式具備大於、小於或等於功能。

oracle decode函式使用方法

(03)該函式的含義如下:IF 條件=值1 THENRETURN(翻譯值1)ELSIF 條件=值2 THENRETURN(翻譯值2)F 條件=值n THENRETURN(翻譯值n)ELSERETURN(預設值)END IF

(04)該函式的含義如下:IF 條件=值1 THENRETURN(翻譯值1)ELSIF 條件=值2 THENRETURN(翻譯值2)F 條件=值n THENRETURN(翻譯值n)ELSERETURN(預設值)END IF

(05)1、比較大小select decode(sign(變數1-變數2),-1,變數1,變數2) from dual; --取較小值sign()函式根據某個值是0、正數還是負數,分別返回0、1、-1例如:變數1=10,變數2=20則sign(變數1-變數2)返回-1,decode解碼結果為“變數1”,達到了取較小值的目的。

(06)2、表、檢視結構轉化現有一個商品銷售表sale,表結構為:month    char(6)      --月份sell    number(10,2)   --月銷售金額現有資料為:200001  1000200002  1100200003  1200200004  1300200005  1400200006  1500200007  1600200101  1100200202  1200200301  1300想要轉化為以下結構的資料:year   char(4)      --年份month1  number(10,2)   --1月銷售金額month2  number(10,2)   --2月銷售金額month3  number(10,2)   --3月銷售金額month4  number(10,2)   --4月銷售金額month5  number(10,2)   --5月銷售金額month6  number(10,2)   --6月銷售金額month7  number(10,2)   --7月銷售金額month8  number(10,2)   --8月銷售金額month9  number(10,2)   --9月銷售金額month10  number(10,2)   --10月銷售金額month11  number(10,2)   --11月銷售金額month12  number(10,2)   --12月銷售金額

(07)結構轉化的SQL語句為:create or replace viewv_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)asselectsubstrb(month,1,4),sum(decode(substrb(month,5,2),'01',sell,0)),sum(decode(substrb(month,5,2),'02',sell,0)),sum(decode(substrb(month,5,2),'03',sell,0)),sum(decode(substrb(month,5,2),'04',sell,0)),

(08)補充1:有學生成績表student,現在要用decode函式實現以下幾個功能:成績>85,顯示優秀;>70顯示良好;>60及格;否則是不及格。假設student的編號為id,成績為score,那麼:select id, decode(sign(score-85),1,'優秀',0,'優秀',-1,decode(sign(score-70),1,'良好',0,'良好',-1,decode(sign(score-60),1,'及格',0,'及格',-1,'不及格')))from student;

(09)補充2:Decode函式的語法結構如下:decode (expression, search_1, result_1)decode (expression, search_1, result_1, search_2, result_2)decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n)decode (expression, search_1, result_1, default)decode (expression, search_1, result_1, search_2, result_2, default)decode (expression, search_1, result_1, search_2, result_2, ...., search_n, result_n, default)decode函式比較表示式和搜尋字,如果匹配,返回結果;如果不匹配,返回default值;如果未定義default值,則返回空值。

(10)以下是一個簡單測試,用於說明Decode函式的用法:SQL> create table t as select username,default_tablespace,lock_date from dba_users;Table > select * from t;USERNAME                        DEFAULT_TABLESPACE              LOCK_DATE------------------------------ ------------------------------ ---------SYS                             SYSTEMSYSTEM                          SYSTEMOUTLN                           SYSTEMCSMIG                           SYSTEMSCOTT                           SYSTEMEYGLE                           USERSDBSNMP                          SYSTEMWMSYS                           SYSTEM                          20-OCT-048 rows > select username,decode(lock_date,null,'unlocked','locked') status from t;USERNAME                        STATUS------------------------------ --------SYS                             unlockedSYSTEM                          unlockedOUTLN                           unlockedCSMIG                           unlockedSCOTT                           unlockedEYGLE                           unlockedDBSNMP                          unlockedWMSYS                           locked8 rows > select username,decode(lock_date,null,'unlocked') status from t;USERNAME                        STATUS------------------------------ --------SYS                             unlockedSYSTEM                          unlockedOUTLN                           unlockedCSMIG                           unlockedSCOTT                           unlockedEYGLE                           unlockedDBSNMP                          unlockedWMSYS8 rows selected.

熱門標籤