函数分为单行函数、多行函数
**
单行函数:一行返回一个值
**
1、日期函数:
sysdate:当前日期
current_date:当前日期
select sysdate from dual;--返回当前系统日期,dual:虚表
select current_date from dual;--返回当前系统日期
add_months:月份相加减
-- 查询所有员工的试用期期到期(转正的日期) 3个月试用期 add_months(hiredate,3)
select hiredate,hiredate+90 from emp;
select hiredate,add_months(hiredate,3) from emp;
months_between(新日期,旧日期):新旧日期之间一共多少个月
-- 查询所有员工到目前为止一共工作了几个月
select months_between(sysdate,hiredate) from emp;
last_day:当前月的最后一天
-- 查询当前月的最后一天
select last_day(sysdate) from dual;
next_day(date,char):下一星期几的日期;char可以是1-7,也可以是星期一到星期日,Monday-Sunday;不过1~7对应的是Sunday-Monday;
-- 下一个星期三是几号
select next_day(sysdate,'星期一') from dual;
to_char(date,‘yyyy-mm-dd hh24:mi:ss’):将日期转换为字符串
select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
select to_char(sysdate,'yyyy"年"mm"月"dd"日" hh12:mi:ss') from dual;
to_date(char,‘yyyy-mm-dd hh24:mi:ss’):将字符串转换为日期
select to_date('2017年7月7日 08:44:33','yyyy"年"mm"月"dd"日" hh12:mi:ss') from dual;
decode(字段,值1,结果1,值2,结果2,…默认结果):如果以上值都不满足条件将返回默认结果
select deptno,dname,loc,decode(deptno,10,'十',20,'二十',30,'三十','haha') 中文名称 from dept;
case 字段 when 值1 then 返回值1 … else 默认值 end:字段值为值1的时候返回值为返回值1…无值的就返回默认值
select deptno,dname,(case deptno when 10 then '十' when 20 then '二十' else '呵呵' end) from dept;
**
多行函数(组函数、聚合函数):
**多行返回一个值;select后出现组函数,就只能和组函数或者分组字段一起使用,不能和单个字段一起使用;另外组函数不能用在where之后,select执行顺序是from–where–group by–having–select–order by,在where出还不能确定组|结果集,无法使用组函数,可以使用在select中和having中
count():求个数
-- 统计一下一共有多少个员工
select count(empno) from emp;
select count(*) from emp;
select count(deptno) from emp;
select count(1) from emp;
select 1 from emp;
sum():求和
-- 计算本公司每个月一共要在工资上花费多少钱
select sum(sal) from emp;
max():求最大值
min():求最小值
-- 查询本公司的最高工资和最低工资
select max(sal),min(sal) from emp;