1、单行函数
– 字符函数,接收字符输入返回字符或者数值,dual是伪表
desc dual;
select * from dual;
2、字符串的连接可以使用concat,也可以用 || ,建议使用 ||
select concat('hello','world') 连接 from dual;
select 'hello' || ',' || 'world' 连接 from dual;
3、字符串截取,使用substr
– 第一个参数是源字符串,第二个参数是开始索引,第三个参数长度,开始的索引使用1和0效果相同
select substr('hello',0,3) from dual;
select substr('hello',1,3) from dual;
select substr('hello',2,3) from dual;
4、获取字符串的长度,使用length
select length('hello') from dual;
5、字符串替换,使用replace
– 第一个参数是源字符串,第二个参数是被替换的字符串,第三个参数是替换的字符串
select replace('hello','l','p') from dual;
数值函数
6、ROUND 四舍五入
select round(3.1415,2) from dual;
select round(3.1440,2) from dual;
select round(3.1446,2) from dual;
select round(3.1449,2) from dual;
select round(3.1450,2) from dual;
select round(3.1466,2) from dual;
select round(45.923,2) from dual;
select round(45.926,2) from dual;
7、TRUNC 截断
select trunc(3.1415826,4) from dual;
select trunc(34.923,2) from dual;
8、MOD 求余
select mod(167,3) from dual;
select mod(167,30) from dual;
9、查询雇员的进入公司的周数
select ename,round((sysdate - hiredate)/7) from emp;
10、获得两个时间段中的月数,MONTHS_BETWEEN()
select ename,round(months_between(sysdate,hiredate)) from emp;
11、获得几个月后的日期,ADD_MONTHS()
– 求出两个月后的日期
select add_months(sysdate,2) from dual;
select empno,ename,to_char(hiredate,'yyyy-mm-dd') from emp;
– 10以下的月前面被补零,可以使用fm去掉前导零
select empno,ename,to_char(hiredate,'fmyyyy-mm-dd') from emp;
12、TO_NUMBER数值转换函数
select to_number('10') + to_number('20') from dual;
select to_char(2345.67,'$9999.9') from dual;
select to_char(2345.67,'L9999.9') from dual;
select to_char(2345.67,'$9999.0') from dual;
select to_char(2342345.67,'$9,999,999.999') from dual;
-- $2,342,345.670
select to_number('$2,342,345.670','$9999999.999') from dual;
-- 2342345.67
select to_number('a','xx') from dual;
-- 10
select to_number('d','xx') from dual;
-- 13
select to_number('1a','xxx') from dual;
-- 26
select to_number('00012345') from dual;
select to_number('34567.89') from dual;
13、TO_DATE日期转换函数
select to_date('2020-10-06','yyyy-mm-dd') from dual;
select to_date('2020-10-06','yyyy/mm/dd') from dual;
select to_date('2020/10/06','yyyy-mm-dd') from dual;
select to_date('2020-10-06','yy-mm-dd') from dual;
select to_date('06-10-2020','dd-mm-yyyy') from dual;
14、查询所有雇员的年薪
select ename 雇员名,sal*12 + comm 年薪 from emp;
15、发现很多员工年薪是null,这是因为null和任何数值计算都是null,下面用nvl处理
select ename 雇员名, nvl(comm,0) 奖金, sal*12 + nvl(comm,0) 年薪 from emp;
17、统计记录数 count()
– 查询所有员工的记录数
select count(*) from emp;
– 不建议使用count(*),可以使用一个具体的列以免影响性能。
select count(ename) 员工数 from emp;
18、最小值查询 min()
– 查询员工最低工资
select min(sal) 最低工资 from emp;
19、最大值查询 max()
– 查询员工最高工资
select max(sal) 最高工资 from emp;
20、查询平均值 avg()
– 查询员工平均工资
select avg(sal) from emp;
21、求和函数 sum()
– 查询20 部门员工工资总和
select '20部门员工工资总和' || sum(sal) from emp e where e.deptno=20;
22、查询各部门人数
select deptno,count(ename) from emp group by deptno;
23、查询每个部门的平均工资
select deptno,avg(sal) from emp group by deptno;
24、查询部门编号和部门下的人数
select deptno,count(ename) from emp;
– 报错
select deptno,ename,count(ename) from emp group by deptno;
25、过滤分组数据 group by … having …
– 查询部门平均工资大于2000的部门
select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;
select deptno,avg(sal) from emp where avg(sal)>8000 group by deptno;
select deptno,avg(sal) from emp where sal>800 group by deptno;
都看到最后了啦,如果觉得写得好的话呐
记得 一键三连 哦!点赞 也行呐!