注意:oracle函数大全文档详见资源处
--dual 伪表 目的:配合查询
select * from dual
select 1+1 from dual
select * from emp
replace--替换
'jagflkascaas' agf 替换成 XXX
select replace('jagflkascaas','agf','XXX') from dual
substr--截取 --开始位置0和1是一样的
'jagflkascaas' 取出gflkascaas
select substr('jagflkascaas',3) from dual --agflkascaas
'jagflkascaas' 取出jag
select substr('jagflkascaas',1,3) from dual
select substr('jagflkascaas',0,3) from dual
concat--连接
'aaaa','bbbb'
select 'aaaa','bbbb' from dual
select 'aaaa'||'bbbb' from dual
select concat('aaaa','bbbb','CCC') from dual --只能有两个参数
length--长度
select length('qwer') from dual
round --四舍五入
select round(56.349) from dual --56
select round(56.549) from dual --57
select round(56.349,2) from dual--56.35
select round(56.349,-1) from dual--60
select round(53.349,-1) from dual--50
trunc--截断
select trunc(56.349) from dual --56
select trunc(56.549) from dual --56
select trunc(56.349,2) from dual--56.34
select trunc(56.349,-1) from dual--50
select trunc(53.349,-1) from dual--50
mod --取余
select mod(10,3) from dual
select mod(10,2) from dual
select mod(10,0) from dual--10
add_months(时间,数值)
当前时间 sysdate
select sysdate from dual
查询3个月以后的时间
select add_months(sysdate,3) from dual
select add_months(sysdate,-3) from dual
select add_months(sysdate,1) from dual
时间是可以相加减的
时间-时间=数值 数值的单位是:天
时间+数值=时间
--计算每个员工的入职天数 -- sysdate:当前系统时间
select empno,ename,hiredate , sysdate-hiredate from emp
--计算每个员工的入职月数
months_between -- 计算两个日期之间的月份差 参数1:当前时间 参数2:要计算的初始时间
select empno,ename,hiredate , months_between(sysdate,hiredate) from emp
to_date
--查询1980-01-01到1985-12-31入职的员工
select * from emp where hiredate between to_date('1980-01-01','yyyy-MM-dd')
and to_date('1985-12-31','yyyy-MM-dd')
to_char
select to_char(sysdate,'yyyy-MM-dd') from dual
select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual
select
to_char(sysdate,'yyyy'),
to_char(sysdate,'mm'),
to_char(sysdate,'dd'),
to_char(sysdate,'day') --星期
from dual
--查询1980和1987入职的员工
select * from emp where to_char(hiredate,'yyyy')='1980' or to_char(hiredate,'yyyy')='1987'
to_number -- 不重要
select 'abc'+'abc' from dual -- 报错
select '123'+'123' from dual
select * from emp where deptno='30'
nvl -- 滤空函数
select sal*12+nvl(comm,0) from emp
decode --类似于条件表达式 只有Oracle有
select job,decode(job,
'CLERK','业务员',
'SALESMAN','销售员',
'其他'
) from emp
--case表达式,重要
语法:case 字段 when 值 then 显示的值.....end
-- 使用表达式
select job,
case job
when 'CLERK' then '业务员'
when 'SALESMAN' then '销售员'
else '其他'
end,
-- 使用decode表达式
decode(job,
'CLERK','业务员',
'SALESMAN','销售员',
'其他'
) from emp
多行函数 聚合函数 组函数
count sum avg max min
分组:group by
查询每个部门的平均工资
select deptno ,avg(sal) from emp group by deptno
查询每个部门的最高工资
select deptno ,max(sal) from emp group by deptno
查询每个部门的最低工资
select deptno ,min(sal) from emp group by deptno
查询每个部门的人数
select deptno ,count(*) from emp group by deptno
查询每个部门的总工资
select deptno ,sum(sal) from emp group by deptno
查询部门的最低工资大于800的部门编号
select deptno ,min(sal) from emp group by deptno having min(sal)>800
where (前)
group by (中)
having (后)