首页 技术 正文
技术 2022年11月11日
0 收藏 501 点赞 2,572 浏览 1938 个字
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;
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839