函数: 必须要有返回值。
分为单行函数和多行函数。
单行函数:对某一行中的某个字段进行处理
数值函数:
字符函数:
日期函数:
转换函数:
通用函数:
多行函数:对某一列的所有行进行处理
max() min count sum avg
注:1.直接忽略空值
----统计员工工资总和
select sum(sal) from emp;
----统计员工奖金总和
select sum(comm) from emp; 2200
----统计员工人数
select count(1) from emp; 14
----统计员工的平均奖金
select avg(comm) from emp; 550 2200/14= 错误,有问题
select sum(comm)/count(1) from emp;
单行函数: select ceil(sum(comm)/count(1)) from emp;
---数值函数----:
select ceil(45.926) from dual; ---46
select floor(45.926) from dual; ---45
round:四舍五入
select round(45.926,2) from dual; ---45.93
select round(45.926,1) from dual; ---45.9
select round(45.926,0) from dual; ---46
select round(45.926,-1) from dual; ---50
select round(45.926,-2) from dual; ---0
select round(65.926,-2) from dual; ---100
----截断
select trnuc(45.926,2) from dual; ---45.92
select trnuc(45.926,1) from dual; ---45.9
select trnuc(45.926,0) from dual; ---45
select trnuc(45.926,-1) from dual; ---40
select trnuc(45.926,-2) from dual; ---0
select trnuc(65.926,-2) 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
--获取字符串长度
select length('abcdefg') from dual; ---7
--去除字符串左右两边的空格 trim
select ' hello ' from dual; ---- hello
select trim(' hello ') from dual; ----hello
--替换字符串 replace
select replace('hello','l','a') from dual; ---heaao
--选择题
select floor(-12.5) from dual; --- -13
select floor(12.5) from dual; ---12
select ceil(-12.5) from dual; ---12
日期函数:
----查询今天的日期
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,'$9999.99') from emp;
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') 返回'1210.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;
---显示24小时制
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
---只想显示年份
select to_char(sysdate,'yyyy') from dual; ----2019
---只想显示天数
select to_char(sysdate,'d') from dual; ----代表一个星期中的第几天
select to_char(sysdate,'dd') from dual; ----代表一个月中的第几天
select to_char(sysdate,'ddd') from dual; ----代表一年中的第几天
select to_char(sysdate,'day') from dual; ----monday
select to_char(sysdate,'dy') from dual; ----mon 星期的简写
字符转日期
select to_date('2019-04-10','yyyy-mm-dd') from emp;
----查询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
select nvl2(null,5,6) from dual; ---6
select nvl2(1,5,6) from dual; ----5
nullif(参数1,参数2) 如果参数1 = 参数2,那么就返回null,否则就返回参数1
select nullif(5,6) from dual; ---5
select nullif(6,6) from dual; ---null
coalesce:返回第一个不为null的值
select coalesce(null,null,3,5,6) from dual;