加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长百科 > 正文

Oracle数据库----函数

发布时间:2021-01-25 06:32:21 所属栏目:站长百科 来源:网络整理
导读:--大小写控制函数 --upper select * from emp where job = upper(‘salesman‘); --lower select * from emp where lower(job) = ‘clerk‘; --initcap select empno,initcap(ename) from emp; --upper、lower、initcap这三个函数的共同点,如果输入参数值为
副标题[/!--empirenews.page--]

--大小写控制函数
--upper
select * from emp where job = upper(‘salesman‘);

--lower
select * from emp where lower(job) = ‘clerk‘;

--initcap
select empno,initcap(ename) from emp;

--upper、lower、initcap这三个函数的共同点,如果输入参数值为null时,则返回null
select empno,initcap(null) from emp;

--字符控制函数

--字符串连接符,实现雇员名与工资两列的连接
select ename || ‘:‘ || sal from emp;

--字符串连接的函数concat,实现雇员名与工资两列的连接
select concat(concat(ename,‘:‘),sal) from emp;

--截串函数 substr
select * from emp where substr(job,1,4) = ‘SALE‘;

--求字符串长度的函数 length
select * from emp where length(ename) = 6;

--instr 求子串在字符串中的位置
select instr(‘hello oracle‘,‘oracle‘) from dual;

select instr(‘hello oracle hello oracle‘,‘oracle‘,2) from dual;

--左填充函数 lpad
select lpad(job,9,‘*‘) from emp;

--右填充函数 rpad
select rpad(job,‘*‘) from emp;

--替换函数 replace
select replace(‘hello oracle‘,‘world‘) from dual;

--四舍五入的函数 round

--求员工的日工资
select round(sal/30,2) from emp;


--截断函数 trunc
select round(sal/30,2),trunc(sal/30,2) from emp;


--求余数的函数 Mod

--求员工号为偶数的员工信息
select * from emp where mod(empno,2)=0;

--sysdate
select sysdate-1 昨天,sysdate 今天,sysdate+1 明天 from dual;

--months_between
select round(months_between(sysdate,hiredate)/12)from emp;

--add_months
select ename,add_months(hiredate,30*12) from emp;

--next_day
select next_day(sysdate,‘星期一‘)from dual;

--last_day
select sysdate,last_day(sysdate) from dual;
select empno,ename,last_day(hiredate) from emp;

--round
select hiredate,round(hiredate,‘YEAR‘),‘MONTH‘) from emp where empno=7654;

--trunc
select hiredate,trunc(hiredate,‘MONTH‘) from emp where empno=7654;

--隐式数据类型转换的举例
select * from emp where sal>‘2000‘;
select * from emp where hiredate=‘02-4月-81‘;

--to_char 日期类型转换成字符类型
select to_char(hiredate,‘YYYY-MM-DD‘) from emp;
select to_char(hiredate,‘YYYY"年"MM"月"DD"日"‘) from emp;
select to_char(hiredate,‘DD-MON-RR‘,‘NLS_DATE_LANGUAGE=AMERICAN‘) from emp;

--to_char 数值类型转换成字符类型
select sal,to_char(sal,‘L0,000,000.00‘)from emp;
select sal,‘L9,999,999.99‘)from emp;
select sal,‘$9,999.99‘)from emp;

--to_date 字符类型转换成日期类型
select ename,hiredate from emp where hiredate>to_date(‘1981-12-31‘,‘YYYY-MM-DD‘);

--to_number 字符类型转换成数值类型
select ename,sal from emp where sal>to_number(‘¥2000‘,‘L99999‘);

--nvl
select ename,sal,comm,sal+nvl(comm,0) from emp;

--nvl2
select ename,nvl2(comm,comm+sal,sal) from emp;

--nullif
select empno,hiredate,nullif(hiredate,trunc(sysdate,‘MONTH‘))from emp;

--coalesce
select ename,coalesce(sal+comm,sal) from emp;

--想显示全部雇员的职位,但是这些职位要求替换为中文显示:
--
--CLERK:办事员;
--SALESMAN:销售;
--MANAGER:经理;
--ANALYST:分析员;
--PRESIDENT:总裁;

--case表达式
select empno,
case job
when ‘CLERK‘ then ‘办事员‘
when ‘SALESMAN‘ then ‘销售‘
when ‘MANAGER‘ then ‘经理‘
when ‘ANALYST‘ then ‘分析员‘
else ‘总裁‘
end
from emp;

--decode函数
select empno,job,decode(job,‘CLERK‘,‘办事员‘,‘SALESMAN‘,‘销售‘,‘MANAGER‘,‘经理‘,‘ANALYST‘,‘分析员‘,‘总裁‘)from emp;

--case表达式,区间值的判断
select empno,
case when sal<2000 then ‘低‘
when sal<5000 then ‘中‘
else ‘高‘
end
from emp;

--参照雇员信息表,想显示距聘用日期3个月后的下一个星期一的日期,且日期格式如:2017-01-06。

select empno,to_char(next_day(add_months(hiredate,3),‘星期一‘),‘YYYY-MM-DD‘) new_date
from emp;

--参照雇员信息表,显示雇员日薪并四舍五入到2 位小数的结果,然后对薪资格式以‘¥ 1,182.19’这样的例子形式进行格式化

select empno,to_char(round(sal/30,999.99‘)
from emp;

--求平均值avg
select avg(sal) from emp;

--求和sum
select sum(sal) from emp;

--求最大值max
select max(sal) from emp;

--求最小值min
select min(sal) from emp;

--计数函数count
select count(*) from emp;
select count(empno) from emp;

--统计一下部门的个数
select count(deptno) from emp;
--在分组函数中通过distinct关键字来去除重复的记录
select count(distinct deptno) from emp;

--行转列wm_concat
select wm_concat(ename) from emp;

--求员工的平均工资
select sum(sal)/count(*) 方法一,sum(sal)/count(sal) 方法二,avg(sal) 方法三 from emp;
--求员工的平均补助
select sum(comm)/count(*) 方法一,sum(comm)/count(comm) 方法二,avg(comm) 方法三 from emp;

select count(*),count(nvl(comm,0)) from emp;

select comm from emp;
--不忽略空值
select avg(nvl(comm,0)) from emp;
--忽略空值
select avg(comm) from emp;

--求出每个部门的平均工资,要求显示:部门号,部门的平均工资
select deptno,avg(sal) from emp group by deptno;

--多列分组
--按不同的部门,不同的职位,计算员工的平均工资
select deptno,avg(sal) from emp group by deptno,job order by deptno;

--求出每个部门的平均工资,要求显示:每个部门的平均工资。
select avg(sal)from emp group by deptno;

--求出每个部门的员工姓名,要求显示:部门编号、员工姓名
select deptno,wm_concat(ename)from emp group by deptno;

--求平均工资大于2500的部门,要求显示:部门号,平均工资

--select deptno,avg(sal) from emp where avg(sal)>2500 group by deptno;

--如果条件中包含分组函数,需要通过having子句对分组结果进行过滤
select deptno,avg(sal) from emp group by deptno having avg(sal)>2500;

--在条件中如果没有分组函数,推荐使用where子句,可以先过滤后分组来提高效率
select deptno,avg(sal) from emp where deptno=10 group by deptno;

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读