多行函数
废话不多说 以SCOTT数据库为例 直接上例子
--统计员工工资总和
select * from emp;
select sum(sal) 工资总和 from emp;
员工表查询结果
统计员工工资总和结果
--统计员工奖金总和
select sum(comm) 奖金总和 from emp;
--统计员工人数
select count(*) 员工人数 from emp;
--求平均值的时候注意空值问题:
select sum(comm)/count(1) 平均奖金 from emp;
这里说一下为什么不用avg()函数的原因,在上图员工表的查询过程中,奖金是存在空值的,而sum()函数将空值忽略,所以求出来的不是正确的平均值。
数值函数
--ceil:向上取整
select ceil(45.926) from dual;--得到 46
select ceil(-45.926) from dual;--得到-45
--floor:向下取整
select FLOOR(45.926) from dual; 45
select FLOOR(-45.926) from dual;-46
--round:四舍五入
select round(45.926) from dual;--默认为round(45.926,0)
select round(45.926,0)from dual; 46
select round(45.926,1)from dual;--45.9 保留小数点后一位进行四舍五入
select round(45.926,2)from dual;--45.93 保留后两位进行四舍五入
select round(45.926,3)from dual;--45.926 保留小数点后三位进行四舍五入
select round(45.926,4)from dual;--45.926 保留小数点后四位进行四舍五入
select round(45.926,-1)from dual;--50 保留小数点前一位进行四舍五入
select round(45.926,-2)from dual; --0 保留小数点前二位进行四舍五入
select round(75.926,-2)from dual;--100 保留小数点前二位进行四舍五入
select round(75.926,-3)from dual;--0 保留小数点前三位进行四舍五入
--截断:trunc 直接截断
select trunc(45.926) from dual;同上述原理相同不论如何直接截断
select trunc(45.926,0)from dual;
select trunc(45.926,1)from dual;--45.9
select trunc(45.926,2)from dual;--45.92
select trunc(45.926,3)from dual;--45.926
select trunc(45.926,4)from dual;--45.926
select trunc(45.926,-1)from dual;--40
select trunc(45.926,-2)from dual; --0
select trunc(75.926,-2)from dual;--0
select trunc(75.926,-3)from dual;--0
--求余
select mod(9,3) from dual;-- 0
select mod(9,4) from dual;-- 1
字符函数
--字符函数
-- substr(str1,起始索引,长度)
--注意: 起始索引不管写 0 还是 1 都是从第一个字符开始截取
select substr('abcdefg',0,3) from dual; --abc
select substr('abcdefg',1,3) from dual; --abc
select substr('abcdefg',2,3) from dual; --bcd
--获取字符串长度 24 28
select length('abcdefg') from dual;
--去除字符左右两边的空格
select trim(' hello ') from dual;
--替换字符串
Select replace('hello','l','a') from dual;
日期函数
--日期函数
--查询今天的日期
select sysdate from dual;
--查询3个月后的今天的日期
select add_months(sysdate,3) from dual;
--查询3天后的日期
select sysdate + 3 from dual;
--查询员工入职的天数
select sysdate - hiredate from emp;
select ceil(sysdate - hiredate) from emp;
--查询员工入职的周数
select (sysdate - hiredate)/7 from emp;
--查询员工入职的月数
select months_between(sysdate,hiredate) from emp;
--查询员工入职的年份
select months_between(sysdate,hiredate)/12 from emp;
--转换函数 数值转字符 字符转数值 日期
--字符转数值 to_number(str) 鸡肋
select 100+'10' from dual; --110 默认已经帮我们转换
select 100 + to_number('10') from dual; --110
--数值转字符
select to_char(sal,'$9,999.99') from emp;
select to_char(sal,'L9,999.99') from emp;
/*
to_char(1210.73, '9999.9') 返回 '1210.7'
to_char(1210.73, '9,999.99') 返回 '1,210.73'
to_char(1210.73, '$9,999.00') 返回 '$1,210.73'
to_char(21, '000099') 返回 '000021'
to_char(852,'xxxx') 返回' 354'
*/
--日期转字符 to_char()
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
--只想要年
select to_char(sysdate,'yyyy') from dual; --2017
--只想要天
select to_char(sysdate,'d') from dual; --2 代表一个星期中第几天
select to_char(sysdate,'dd') from dual; --10 代表一个月中的第几天
select to_char(sysdate,'ddd') from dual; --100 代表一年中的第几天
select to_char(sysdate,'day') from dual; --monday
select to_char(sysdate,'dy') from dual; --mon 星期的简写
--字符转日期
select to_date('2017-04-10','yyyy-mm-dd') from dual;
--查询1981年 -- 1985年入职的员工信息
select * from emp where hiredate between to_date('1981','yyyy') and to_date('1985','yyyy');
通用函数
/*
通用函数:
nvl(参数1,参数2) 如果参数1 = null 就返回参数2
nvl2(参数1,参数2,参数3) 如果参数1 = null ,就返回参数3, 否则返回参数2
nullif(参数1,参数2) 如果参数1 = 参数2 那么就返回 null , 否则返回参数1
coalesce: 返回第一个不为null的值
*/
select nvl2(null,5,6) from dual; --6;
select nvl2(1,5,6) from dual; --5;
select nullif(5,6) from dual; --5
select nullif(6,6) from dual; --null
select coalesce(null,null,3,5,6) from dual; --3
oracle函数学习视频免费分享,私聊哦。