oracle中decode的使用

含义解释:
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来判断

 

参考:http://www.imooc.com/qadetail/197897

posted @ 2021-01-07 10:08  masha2017  阅读(2191)  评论(0编辑  收藏  举报