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;

oracle中的条件语句的更多相关文章

  1. js中的条件语句

    //js中的条件语句 ; //example1 单分支语句 ){ console.log("你已经不年轻了!"); }else{ console.log("你依然很有活力 ...

  2. Mysql中的条件语句if、case

    Mysql中的条件语句在我们对数据进行转换的时候比较有用,这样就不需要创建中转表. IF 函数 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> ...

  3. Oracle中分页查询语句

    Oracle分页查询语句使我们最常用的语句之一,下面就为您介绍的Oracle分页查询语句的用法,如果您对此方面感兴趣的话,不妨一看. Oracle分页查询语句基本上可以按照本文给出的格式来进行套用.O ...

  4. java中的条件语句(if、if...else、多重if、嵌套if)

    Java条件语句之 if 生活中,我们经常需要先做判断,然后才决定是否要做某件事情.例如,如果考试成绩大于 90 分,则奖励一个 IPHONE 5S .对于这种"需要先判断条件,条件满足后才 ...

  5. Oracle 中按条件过滤重复记录

    在数据处理中,经常会遇到类似这样的情况:数据库中存在多条记录,其中某些字段值相同,其他字段值不同.实际的业务需要针对这样的情况,只保留一条数据,其他数据删除.如何做到呢?在sql中有top关键字相对容 ...

  6. 【Python】解析Python中的条件语句和循环语句

    1.if语句 if语句有好几种格式,比如: if condition: statement 使用 if ... else ...: if condition: statement(1) else: s ...

  7. oracle中查看sql语句的执行计划

    1.在pl/sql中打开cmd命令容器 2.在cmd命令窗口中输入:explain plan for select * from t; 3.查看sql语句的执行计划:select * from tab ...

  8. mybatis sql中的条件语句

    1.mybatis判断是否为空或null <if test="type!=null and type!=''"> AND type = #{type} </if& ...

  9. [译]javascript中的条件语句

    本文翻译youtube上的up主kudvenkat的javascript tutorial播放单 源地址在此: https://www.youtube.com/watch?v=PMsVM7rjupU& ...

随机推荐

  1. linux 用户管理 groupadd、groupmod、groupdel、gpasswd

    添加用户组groupadd [选项] 组名 /usr/sbin/groupadd执行权限:root一个用户可以属于多个所属组,但有一个缺省组,和用户名同名-g GID:指定组ID 修改用户组 grou ...

  2. web 应用请求乱码问题

    背景 作为非西欧语系的国家,总是要处理编码问题 使用java编码解码 @Test public void coderTest() throws UnsupportedEncodingException ...

  3. 如何drop大表的中不用的字段 set unused column

    转自 http://foxmile.blog.163.com/blog/static/81169805201143191957184/ 我 们要删除表中不用的字段,如果直接drop column,对于 ...

  4. Debian出现in the drive ‘/media/cdrom/’ and press enter解决办法

    没有光盘源解决打开/etc/apt/sources.list文件,注释掉cdrom那一行,然后再执行apt-get update更新下deb仓库这样以后再使用apt-get安装时就不会再搜寻cdrom ...

  5. leetCode题解之First Missing Positive

    1.问题描述 2.题解思路 本题的思路是对于数组中每个正的元素,应该将其放到数组中对应的位置,比如元素1 ,应该放在数组的第一个位置.以此类推,最后检查数组中元素值和下标不匹配的情况. 3.代码 in ...

  6. VisualSVN Server迁移的方法

    VisualSVN Server迁移涉及到两种情况: 第一种情况:VisualSVN Server没有更换电脑或者服务器,只是修改Server name. 第二种情况:当VisualSVN Serve ...

  7. 使用Reflector反编译并提取源代码

    Reflector是一个强大的.net 反编译工具,有时我们不止需要反编译源代码,更需要提取源代码. Reflector本身不自带提取源代码功能,不过可以借助插件Reflector.FileDisas ...

  8. django新建项目,连接mysql数据库

    安装django,进入Django目录,运行 python setup.py install 在workplace目录下新建一个名为site01的项目: cd workplacedjango-admi ...

  9. spine获取骨骼位置

    time: 2015/07/23 版本: /****************************************************************************** ...

  10. Leuze BCL308i 使用方法整理

    1 硬件连接关系 1.1 接口盒 BCL308i一般选配MK308/MK348/MK358系列接口盒, 单独使用(不组成扫描集群)时需要连接3根线,分别为SERVICE .SW/PWR.HOST/BU ...