含義解釋:
decode(條件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)
例如:
select decode(1,1,'第一個',2,'第二個',3,'第三個','沒有') from dual
返回:第一個
其作用相當於case...when...
select case 1 when 1 then '第一個' when 2 then '第二個' when 3 then '第三個' else '沒有' end results from dual
decode 與 case..when..區別:
1.DECODE 只有Oracle 才有,其它數據庫不支持;
2.CASE WHEN的用法, Oracle、SQL Server、 MySQL 都支持;
3.DECODE 只能用做相等判斷,但是可以配合sign函數進行大於,小於,等於的判斷,CASE when可用於=,>=,<,<=,<>,is null,is not null 等的判斷;
4.DECODE 使用其來比較簡潔,CASE 雖然復雜但更為靈活;
5.另外,在decode中,null和null是相等的,但在case when中,只能用is null來判斷