1,大小写转换
select upper('Hello') 转大写,lower('Hello') 转小写,initcap('hello') 首字母大写 from dual;
2,截取字符串
--从第二位开始取
select substr('hello',2) from dual; --ello
--从第二位开始取3位
select substr('hello',2,3) from dual; --ell
--第一个o的位置
select instr('hello world','o') 位置 from dual;--5
--长度
select length('你好') 字符,lengthb('你好') 字节 from dual; --2,4
3,替换指定字符
select replace('Hello World' ,'l','*') from dual; --He**o Wor*d
4,四舍五入
select round(45.926,2) 两位小数,round(45.926,1) 一位小数 from dual;
select trunc(45.926,2) 保留二位,trunc(45.926,1) 保留一位 from dual;---不四舍五入
5,时间
select sysdate from dual;
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
select add_months(sysdate,56) from dual;--56 个月后
select last_day(sysdate) from dual;
select next_day(sysdate,'星期日') from dual;--下一个星期日
6,nvl2(a,b,c) 当a=null的时候,返回c;否则返回b
select sal*12+nvl2(comm,comm,0) from emp;
nullif(a,b) 当a=b的时候,返回null;否则返回a
select nullif('abc','abc') 值 from dual;
7,分页查询
select *
from (select rownum r,e1.*
from (select * from emp order by sal) e1
where rownum <=8
)
where r >=5;
8,创建视图
create or replace view empinfoview
as
select e.empno,e.ename,e.sal,e.sal*12 annsal,d.dname
from emp e, dept d
where e.deptno=d.deptno
with read only;
9,创建索引
create index myindex
on emp(deptno);
10,创建同义词
扫描二维码关注公众号,回复:
11627535 查看本文章
create synonym myemp1 for hr.employees;
11,
declare
cursor cemp is select empno,empjob from emp;
pempno emp.empno%type;
pjob emp.empjob%type;
begin
open cemp;
loop
--取一条记录
fetch cemp into pempno,pjob;
exit when cemp%notfound;
--判断职位
if pjob = 'PRESIDENT' then update emp set sal=sal+1000 where empno=pempno;
elsif pjob = 'MANAGER' then update emp set sal=sal+800 where empno=pempno;
else update emp set sal=sal+400 where empno=pempno;
end if;
end loop;
close cemp;
commit;
dbms_output.put_line('完成');
end;