decode()函數簡介:
主要作用:將查詢結果翻譯成其他值(即以其他形式表現出來,以下舉例說明);
使用方法:
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等;
舉例說明:
現定義一table名為output,其中定義兩個column分別為monthid(var型)和sale(number型),若sale值=1000時翻譯為D,=2000時翻譯為C,=3000時翻譯為B,=4000時翻譯為A,如是其他值則翻譯為Other;
SQL如下:
Select monthid , decode (sale,1000,'D',2000,'C',3000,'B',4000,'A',’Other’) sale from output
摘自:https://www.cnblogs.com/freespider/archive/2010/08/09/1795977.html