Oracle中函数的用法

一、round


Oracle round函数是什么意思?怎么运用?

如何使用 Oracle Round 函数 (四舍五入)
描述 : 传回一个数值,该数值是按照指定的小数位元数进行四舍五入运算的结果。
SELECT ROUND( number, [ decimal_places ] ) FROM DUAL
参数:
number : 欲处理之数值
decimal_places : 四舍五入 , 小数取几位 ( 预设为 0 )
Sample :
select round(123.456, 0) from dual;          回传 123 
select round(123.456, 1) from dual;          回传 123.5 
select round(123.456, 2) from dual;          回传 123.46 
select round(123.456, 3) from dual;          回传 123.456 
select round(-123.456, 2) from dual;        回传 -123.46



二、Coalesce


Coalesce:返回表达式中第一个非空的表达式,当所有表达式都为null的时候返回null,该函数最少要有2个参数。是nvl()函数的扩展。
 
语法:
COALESCE(expr1,expr2...exprn);
 
解释
COALESCE(expr1,expr2,expr3);
首先判断expr1是否为null,如果不为null,则取expr1的值,如果为null的话则去判断expr2是否为null。一次类推判断到最后,如果所有表达式都为null,则为null。
上一句话等同于:
select case when expr1 is not null then expr1 else
          case when expr2 is not null then expr2 else
          case when expr3 is not null then expr3 else nll end end end  from tabl;
 
举例:
drop table tbl_coalesce purge;
创建表:
create table tbl_coalesce (name varchar2(24),age varchar2(4));
插入数据:
insert into tbl_coalesce(name,age) values('sunmi','26');
insert into tbl_coalesce(name,age) values('rain','32');
insert into tbl_coalesce(name,age) values(null,'2');
insert into tbl_coalesce(name,age) values('dada',null);
insert into tbl_coalesce(name,age) values(null,null);
使用coalesce函数:
select coalesce(name,age,'is null') from tbl_coalesce;
使用普通sql语句:
select case when name is not null then name else case when age is not null then age else 'is null'end  end from tbl_coalesce;
结果:
COALESCE(NAME,AGE,'ISNULL')
---------------------------
sunmi
rain
2
dada
is null
结论:当name和age都为null的的时候,赋值为‘is null’

猜你喜欢

转载自blog.csdn.net/weixin_42387852/article/details/80760092