SQL中 decode 函式介紹

2021-09-03 01:37:11 字數 972 閱讀 3749

decode() 函式的語法:

1 select decode(columnname,值1,翻譯值1,值2,翻譯值2,...值n,翻譯值n,預設值)

2 3 from talbename

4 5 where …

其中:columnname為要選擇的table中所定義的column;

預設值可以是你要選擇的column name本身,也可以是你想定義的其他值,比如other等;

主要作用:相當於if語句, 將查詢結果翻譯成其他值。(即以其他形式表現出來)。

舉例說明:

現定義一table名為output,其中定義兩個column分別為monthid(var型)和sale(number型),若sale值=1000時翻譯為d,=2000時翻譯為c,=3000時翻譯為b,=4000時翻譯為a,如是其他值則翻譯為other:

select monthid , decode (sale,1000,'d',2000,'c',3000,'b',4000,'a',』other』) sale from output
若只與乙個值進行比較:

select monthid ,decode(sale, null,『---』,sale) sale from output
decode中可使用其他函式,如nvl()函式或sign()函式等:

nvl(expr1,expr2)

若expr1是null,則返回expr2,否則返回expr1。

如果用到decode函式中:

select monthid,decode(nvl(sale,6000),6000,'ng','ok') from output;
sign()函式根據某個值是0、正數還是負數,分別返回0、1、-1,

用如下的sql語句取較小值:

select monthid,decode(sign(sale-6000),-1,sale,6000) from output;

SQL中 decode 函式簡介

今天看別人的sql時看這裡面還有decode 函式,以前從來沒接觸到,上網查了一下,還挺好用的乙個函式,寫下來希望對朋友們有幫助哈!decode 函式簡介 主要作用 將查詢結果翻譯成其他值 即以其他形式表現出來,以下舉例說明 使用方法 select decode columnname,值1,翻譯值1...

SQL中的decode 函式

decode 函式是sql中比較常見的函式,主要用於將查詢結果翻譯成其他值,下面將為您介紹sql中decode 函式,供您參考。decode 函式簡介 主要作用 將查詢結果翻譯成其他值 即以其他形式表現出來,以下舉例說明 使用方法 select decode columnname,值1,翻譯值1,值...

SQL中 decode 函式簡介

decode函式,是oracle公司的sql 軟體oracle pl sql所提供的特有函式計算方式,以其簡潔的運算方式,可控的資料模型和靈活的格式轉換而聞名。今天看別人的sql時看這裡面還有decode 函式,以前從來沒接觸到,上網查了一下,還挺好用的乙個函式,寫下來希望對朋友們有幫助哈!deco...