oracle一些常用函数用法

来源:互联网 发布:网络规划设计师教材 编辑:程序博客网 时间:2024/06/05 00:39

出自 http://www.iteye.com/topic/602339

SUBSTR(string,start,count)
取子字符串,从start开始,取count个
SQL> select substr('13088888888',3,8) sub from dual;

SUB
--------
08888888


UPPER
返回字符串,并将所有的字符大写
SQL> select upper('AaBbCcDd') upper from dual;

UPPER
--------
AABBCCDD


LOWER
返回字符串,并将所有的字符小写
SQL> select lower('AaBbCcDd')AaBbCcDd from dual;

AABBCCDD
--------
aabbccdd


FLOOR
对给定的数字取整数
SQL> select floor(2345.67) fl from dual;

FL
--------------
2345


ROUND
按照指定的精度进行舍入
SQL> select round(55.5) roA,round(-55.4) roB from dual;

ROA     ROB
----------- ------------
56          -55 


SIGN
取数字n的符号,大于0返回1,小于0返回-1,等于0返回0
SQL> select sign(123) A,sign(-100) B,sign(0) C from dual;

A      B         C
--------- ---------- ---------
1         -1         0


.ADD_MONTHS
增加或减去月份
SQL> select to_char(add_months(to_date('199912','yyyymm'),2),'yyyymm') A from dual;

A
------
200002
SQL> select to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm')  A from dual;

A
------
199910


SYSDATE
用来得到系统的当前日期
SQL> select to_char(sysdate,'dd-mm-yyyy day') A  from dual;

A

-----------------
09-05-2004 星期日

TO_CHAR(date,'format')
SQL> select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') A from dual;

A
-------------------
2004/05/09 21:14:41


TO_NUMBER
将给出的字符转换为数字
SQL> select to_number('1999') year from dual;

YEAR
---------
 1999


AVG(DISTINCT|ALL)
all表示对所有的值求平均值,distinct只对不同的值求平均值
SQLWKS> create table table3(xm varchar(8),sal number(7,2));
语句已处理。
SQLWKS>  insert into table3 values('gao',1111.11);
SQLWKS>  insert into table3 values('gao',1111.11);
SQLWKS>  insert into table3 values('zhu',5555.55);
SQLWKS> commit;

SQL> select avg(distinct sal) A from gao.table3;

A
----------------
3333.33

SQL> select avg(all sal) from gao.table3;

A
-----------
2592.59


GROUP BY
主要用来对一组数进行统计
SQL> select deptno,count(*) ,sum(sal) from scott.emp group by deptno;

DEPTNO  COUNT(*)  SUM(SAL)
--------- --------- ---------
10         3      8750
20         5     10875
30         6      9400


HAVING
对分组统计再加限制条件
SQL> select deptno,count(*),sum(sal) from scott.emp group by deptno having count(*)>=5;

   DEPTNO  COUNT(*)  SUM(SAL)
--------- --------- ---------
       20         5     10875
       30         6      9400
SQL> select deptno,count(*),sum(sal) from scott.emp having count(*)>=5 group by deptno ;

   DEPTNO  COUNT(*)  SUM(SAL)
--------- --------- ---------
       20         5     10875
       30         6      9400


ORDER BY
用于对查询到的结果进行排序输出
SQL> select deptno,ename,sal from scott.emp order by deptno,sal desc;

   DEPTNO ENAME            SAL
--------- ---------- ---------
       10 KING            5000
       10 CLARK           2450
       10 MILLER          1300
       20 SCOTT           3000
       20 FORD            3000
       20 JONES           2975
       20 ADAMS           1100
       20 SMITH            800
       30 BLAKE           2850
       30 ALLEN           1600
       30 TURNER          1500
       30 WARD            1250
       30 MARTIN          1250
       30 JAMES            950





 



1 0
原创粉丝点击