Oracle基础知识(6)--函数

--oracle的函数
--函数可以嵌套调用
*字符函数:处理字符串
--ascii chr
select ascii('a') from dual;
select chr(97) from dual;
--lower,upper
--求A的ASCII码
select ascii(upper('a')) from dual;
--求a的ASCII码
select ascii(lower('A')) from dual;


--intcap:第一个字符大写,其他字符小写
select initcap(ename) from emp;


--ltrim,rtrim,trim
--ltrim,rtrim是字符级别的截取,在截断的时候,按照字符去匹配
select ltrim('elaleellen','el') from dual; 
select rtrim('ellenellneen','ne') from dual;
--trim的截取集是一个字符
select trim('e' from 'ellenellnee') from dual;
select trim('   ellen   ')from dual;--去掉两边的空格


--lpad,rpad:字符串
select lpad('hao',2,'ni') from dual;
select lpad('hao',5,'ni') from dual;
select lpad('hao',9,'ni') from dual;


select rpad('ni',1,'hao') from dual;
select rpad('ni',5,'hao') from dual;
select rpad('ni',8,'hao') from dual;


--其他:concat,length,substr,replace
--字符串链接,和连接运算符作用接近
select concat('Dear ',ename) from emp;
--length:查询empt表中员工姓名为5个字符的员工的信息
select * from emp where length(ename)=5;
--substr:求子串
select substr('hello world',3,5) from dual;
select substr('hello world',3) from dual;
--replace:替换
select replace('shelly','el','en') from dual;


*数字函数
--sign:求符号
select sign(-5) from dual;
--ceil:向上取整
select ceil(5.6) from dual;
--floor:向下取整
select floor(5.6) from dual;
--round:四舍五入
select round(3.5) from dual;
select round(356,-2) from dual;
--trunc:截断
select trunc(3.1415926,4) from dual;
select trunc(356,-2) from dual;


*日期函数
--sysdate systimestamp
select sysdate from dual;
select systimestamp from dual;
--add_months
select add_months(sysdate,1) from dual;
--months_between(date,date)
select months_between(sysdate,hiredate) from emp;
select months_between(sysdate,add_months(sysdate,1)) from dual;
--last_day(date)
select last_day(sysdate) from dual;
select last_day('08-2月-2018') from dual;
--next_day(date,char)
select next_day(sysdate,'星期一') from dual;
--round,trunc
select trunc(sysdate,'MONTH') from dual;
select round(sysdate,'MONTH') from dual;
select round(date'2018-01-16','MONTH') from dual;
--extract:
select extract(year from sysdate) as 年,
extract(month from sysdate) as 月,
extract(day from sysdate) as 日,
extract(hour from systimestamp) as 时,
extract(minute from systimestamp) as 分,
extract(second from systimestamp) as 秒
from dual;


*转换函数
--to_char to_date
--nvl nvl2
--问题:对emp表中员工,如果奖金为空,给200元奖金,如果奖金不为空,那么在原有奖金基础上加100
update emp01 set comm=nvl2(comm,comm+100,200);
--问题:查询emp表中所有员工的月收入
select ename,sal+nvl(comm,0) as 月收入 from emp;
select ename,coalesce(sal+comm,sal) as 月收入 from emp;
--decode
--案例:按照职位提升工资,如果是manager,工资是原来的1.5倍,如果是ANALYST,工资是原来的1.2倍
--如果是SALESMAN,工资是原来的1.1倍,否则工资是原来的1.05倍
update emp01 set sal=decode(job,'MANAGER',sal*1.5,
'ANALYST',sal*1.2,'SALESMAN',sal*1.1,sal*1.05);

你可能感兴趣的:(Oracle基础知识(6)--函数)