Oracle常用函数

来源:互联网 发布:linux apache访问权限 编辑:程序博客网 时间:2024/05/29 04:22

--oracle之常用函数

trunc();--四舍五不入

select trunc(89745.43) from dual;--结果:89754
select trunc(89745.43,1) from dual;--结果:89745.40
select trunc(89745.46,1) from dual;--结果:89745.40
select trunc(89745.46,-1) from dual;--结果:89740

round(
);--四舍五入
select round(89745.43) from dual;--结果:89754
select round(89745.43,1) from dual;--结果:89745.40
select round(89745.46,1) from dual;--结果:89745.50
select round(89745.46,-1) from dual;--结果:89750

upper();--小写转大写
select upper('Hello') from dual;--结果:HELLO

lower();--小写转大写
select lower('Hello') from dual;--结果:hello

substr();--字符串截取
select substr('Hello',3) from dual;--结果:llo
select substr('Hello',3,2) from dual;--结果:ll
select substr('Hello',-1) from dual;--结果:o

--日期函数
months_between();--两年之间共有多少个月
add_months();--月份加
last_day();--一个月中的最后一天
next_day();--下一个准确日
--查询emp表中员工从雇佣日期到今天有多少年?
select trunc(months_between(sysdate,hiredate)/12) years from emp;
--查询emp表中员工从雇佣日期到今天有多少月?
select trunc(mod(months_between(sysdate,hiredate),12)) months from emp;
--查询emp表中员工编号,姓名,雇佣日期,从雇佣日期到今天有多少年、月、日?(考虑闰年和闰月的情况)
select
       empno,ename,hiredate,
       trunc(months_between(sysdate,hiredate)/12) years,
       trunc(mod(months_between(sysdate,hiredate),12)) months,
       trunc(sysdate-add_months(hiredate,months_between(sysdate,hiredate))) day
from emp;

--三个转换函数:to_char()、to_date()、to_number()
select to_char(sysdate,'yyyy-mm-dd') ch_year from dual;
select to_date('1999-04-23','yyyy-mm-dd') ch_date from dual;

select to_number('123',999) from dual ch_number;



--nvl():语法【nvl('列名',数字)】
select empno,ename,comm,sal,comm*sal*12 income from emp;
--计算员工的年收入
select empno,ename,comm,sal,nvl(comm,0),(sal+nvl(comm,0))*12 income  from emp;


--decode():语法【decode (列名,'列名值1','显示的值1',,'列名值2','显示的值2',...['默认值'])】
--部门用中文显示
select empno,ename,comm,sal,
decode(job,'ANALYST','分析员','CLERK','检查员','SALESMAN','销售员','MANAGER','经理','没有此职位') from emp;