您的位置:首页 > 数据库 > Oracle

Oracle 常用的函数

2016-05-30 11:58 423 查看
/*常用的伪列有ROWID和ROWNUM*/
select rowid, orders.* from orders;

--orders表的结果

create table ORDERS

(

  c1  NUMBER(5) not null,

  c10 NUMBER(20) not null,

  c20 VARCHAR2(20) not null

)

--orders表的数据

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (2, 3, 'oop');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

insert into ORDERS (c1, c10, c20)

values (2, 3, 'oop');

insert into ORDERS (c1, c10, c20)

values (2, 3, 'oop');

insert into ORDERS (c1, c10, c20)

values (2, 3, 'oop');

insert into ORDERS (c1, c10, c20)

values (2, 3, 'oop');

insert into ORDERS (c1, c10, c20)

values (1, 2, 'sa');

commit;

--1)重复查询数据

  select *from orders group by c1,c10,c20

  

  select distinct * from orders;

  select * from orders o where rowid=

  (select max(rowid) from orders oo where oo.c1=o.c1 and oo.c10=o.c10 and oo.c20=o.c20)

  

   select * from orders o where rowid in

  (select max(rowid) from orders group by c1,c10,c20)

  

  --2)删除重复的数据

  select rowid, orders.* from orders;

  

  delete from orders 

  where rowid not in

  (select max(rowid) from orders group by c1,c10,c20)

  

/*  集合操作符

UNION(联合)

UNION ALL(联合所有)

INTERSECT(交集)

MINUS(减集)*/

如果选择列表中包含有表达式或者函数,那么必须为表达式或函数定义列别名

--1、Uinon:无重并集,并以第一列的结果进行升序排序

select *from emp where deptno=10

union 

select * from emp where sal>500

--2、Uinon all:有重并集,不对结果集排序

select *from emp where deptno=10

union all

select * from emp where sal>500

--3、Intersect:交集,只返回两个查询共同返回的行

select *from emp where deptno=10

intersect 

select * from emp where sal>500

--4、Minus:差集,采用第一个查询返回的行,减去第二个查询中也同样返回的行,最后返回剩下的行

select * from emp where sal>500  --15 

minus 

select *from emp where deptno=10  --3

/*连接操作符*/

select 'aaa'||1001 from dual

--(一)转换函数将值从一种数据类型转换为另一种数据类型

--常用的转换函数

--TO_CHAR (X[[c2],c3])【功能】将日期或数据转换为char数据类型

/*x是一个date或number数据类型。

c2为格式参数

c3为NLS设置参数*/

select to_char(1210.73, '9999.999') from dual;

select to_char(1210.73, '$9,999.00') from dual;

select to_char(sysdate,'yyyy-MM-dd'),to_char(sysdate,'DL'),to_char(sysdate,'DS')from dual;

--TO_DATE(X[,c2[,c3]])【功能】将字符串X转化为日期型

select to_date('201607','yyyymm'),to_date('2008-12-31 12:31:30','yyyy-mm-dd hh24:mi:ss')

 from dual;

--TO_NUMBER(X[[,c2],c3])【功能】将字符串X转化为数字型

select TO_NUMBER('199912')+10,TO_NUMBER('450.05') from dual;

--查询雇佣日期》1981 的员工信息

select * from emp;

--oracle有默认的日期格式(日-月-年)

select * from emp where hiredate>'20-12月-1981'

--to_char

select * from emp where to_char(hiredate,'DS')>'1982-01-23'

--员工编号与姓名连在一起

select empno||'_'||ename,sal+comm from emp;

/*转换空值的函数

NVL 

NVL2

DECODE*/

/*【语法】NVL (expr1, expr2)

【功能】若expr1为NULL,返回expr2;expr1不为NULL,返回expr1。*/

select nvl('','xx'),nvl('aa','xx') from dual;

 select * from orders 

 

 select c20,nvl(c20,'empty') from orders;

 

/*【语法】NVL2 (expr1, expr2, expr3) 

【功能】expr1不为NULL,返回expr2;expr2为NULL,返回expr3。
expr2和expr3类型不同的话,expr3会转换为expr2的类型 */

  select nvl2('','xx','John'),nvl2('aa','xx','John') from dual;

  select c20,nvl2(c20,c20,'NULL') from orders;

/*decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)

【功能】根据条件返回相应值

【参数】c1, c2, ...,cn,字符型/数值型/日期型,必须类型相同或null

注:值1……n 不能为条件表达式,这种情况只能用case when then end解决*/

select c20,decode(c20,'oop','OOP','sa','SA','aa','AA','EMPTY') from orders;

select emp.* ,decode(deptno,'10','财务部','20','公关部','30','销售部') from emp;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: