decode,case when  有什么区别?各自的用法?

解决方案 »

  1.   

    case when比decode功能要强大,等到你遇到decode实现不了的功能,你就想到了case when了。
      

  2.   

    decode 是等值比较,使用语法简单,不用写太长。case when 有两比较方式,但是语法上要复杂一些。
    -- 条件非常灵活
    case 
    when 条件1 then 值1
    when 条件2 then 值2
    else 值3
    end-- 等同于 decode 
    case 值
    when 值1 then 值11
    when 值2 then 值22
    else 值33
    end
      

  3.   

    1.对null的处理不同
    2.decode要敲的字少
      

  4.   

    case when比decode更强大,适合多条件,且条件比较复杂的情况。
    如果条件比较简单decode,会使代码更简洁。
      

  5.   

    对一个特定值比较用decode,范围比较用case when
      

  6.   

    补充:case值when方式时,这里的decode(null,null,1)可以返回1,但是case null when null then 1 end 不能返回1
      

  7.   

    我觉得decode 就是缩减版本的case when,判断逻辑简单用decode ,判断逻辑复杂用case when,case when 3个以上判断代码可读性更高。
      

  8.   

    这个没啥好纠结的,decode是ORACLE特有的写法方便,而case when是基于sql语法标准的写法,基本所有数据库都支持
      

  9.   

    case when  很大优点是 when 后面可以是表达式,而decode判断只能用具体的值。
      

  10.   

    case when 功能比decode 强大。 
    decode可以实现的功能,case when 都可以。但是,反之不一定。
      

  11.   

    Case when 理解性,可读性比较好,一看就知道逻辑,且Oracle 和MS SQL 都可用
    decode 则要想一下。
    同事用 decode 写了一堆 decode 组句,过了一星期要debug,已无法判别了,费了好多时间才改好