• oracle中的条件语句


    oracle中case when then及decode用法
    一.case … when … then 语法: 
    – 写法一: 
    case(条件) 
    when 值1 then 返回值1 
    when 值2 then 返回值2 
    else 缺省值 
    
    – 写法二: 
    case when 条件1 then 返回值1 
    when 条件2 then 返回值2 
    else 缺省值 
    end;
    
    案例1:
    -- 如果部门编号为10的,显示为dept10
    -- 如果部门编号为20的,显示为dept20
    -- 如果部门编号为30的,显示为dept30
    -- 否则显示为other
    -- 这一列查询的结果,列名显示为 department
    
    使用
    写法一:
    select ename,
           sal,
           case deptno
             when 10 then
              'dept10'
             when 20 then
              'dept20'
             when 30 then
              'dept30'
             else
              'other'
           end department
      from emp
    
    写法二:
    select ename,
           sal,
           case
             when deptno = 10 then
              'dept10'
             when deptno = 20 then
              'dept20'
             when deptno = 30 then
              'dept30'
             else
              'other'
           end department
      from emp
    
    在这个例子中条件都是等值,结果是一样的。
    如果是不等值的或是有多个表达式,就只能用第二种了,比如:
    select ename,sal,
    case when deptno= 10 or deptno = 20 or deptno = 30 then 'dept'||deptno end dept
    from emp;
    
    select ename,sal,
    case when deptno<=20 then 'dept'||deptno end dept
    from emp;
    
    select ename,
           sal,
           case
             when sal > 0 and sal <= 1500 then
              'level1'
             when sal > 1500 and sal <= 2500 then
              'level2'
             when sal > 2500 and sal <= 4500 then
              'level3'
             else
              'level4'
           end sal_level
      from emp
     order by sal desc;
    
    二.decode函数: 
    decode(条件,值1,返回值1,值2,返回值2,…….,缺省值)
    使用decode函数来实现案例1:
    select ename,
           sal,
           decode(deptno, 10, 'dept10', 20, 'dept20', 30, 'dept30', 'other') department
      from emp
    
    查出来的结果和上面用case when一样,但这句看起来简洁得多了
    
    decode()函数的延伸用法:
    1.与sign函数联用比较大小:
    --get arg1与arg2的较小值 
    语法:select decode(sign(arg1-arg2),-1,arg1,arg2) from dual;  
    实例:select decode(sign(3-5),1,3,5) from dual --5 
    注:sign()函数根据某个值是0、正数还是负数,分别返回0、1、-1
    
    一个Update语句:把表中某个字段的值进行更改,这条语句是把值为“1”的都改成“8”,“0”改成“9”
    update tablename set 字段名= decode(字段名,1,8,0,9)  where 字段名 in (1, 0);
    三、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来判断
    示例如下:
      emp表中有一列comm,如果这列为null,则显示为0,否则,显示为原值:
    --decode可以显示我们要求的结果
    SQL> select ename,decode(comm,null,0,comm) comma from emp;
    -- null没有转成0,仍然是null,不是我们要求的结果
    SQL> select ename,(case comm when null then 0 else comm end) comm from emp;
    --这样才可以成功将null显示为0
    SQL> select ename,(case when comm is null then 0 else comm end) comm from emp;
    作者:demon09
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    有关WCSF的几点整理
    应用Response.Write实现带有进度条的多文件上传
    使用Response.Write实现在页面的生命周期中前后台的交互
    多线程实现Thread.Start()与ThreadPool.QueueUserWorkItem两种方式对比
    LinQ to SQL 及 non-LinQ方式实现Group的Performance对比
    Object.assign()方法
    继承
    面对对象和原型链
    画布实现验证码
    日期的格式 字符串转日期
  • 原文地址:https://www.cnblogs.com/demon09/p/sql.html
Copyright © 2020-2023  润新知