Oracle (字符串函数,数学函数,日期函数,逻辑运算函数,其他函数)

          
SQL中的单记录函数:
1.ASCII 返回与指定的字符对应的十进制数;
SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii('') apace from dual;

2.CHR 给出整数,返回对应的字符;
SQL> select chr(54740) zhao,chr(65) chr65 from dual;

3.CONCAT 连接两个字符串;
SQL> select concat('010-','67126565') || '-6565' from dual;

4.INITCAP 返回字符串并将字符串的第一个字母变为大写;
SQL> select initcap('renhm') ren from dual;

5.INSTR(C1,C2,I) 在一个字符串中搜索指定的字符,返回发现指定的字符的位置;
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
 
SQL> select instr('oracle tring','c','1') tring from dual;

6.LENGTH 返回字符串的长度;
SQL> select length(ename),length(job) from emp;

LENGTH(ENAME) LENGTH(JOB)
------------- -----------
            5           5
            5           8
            4           8

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

LOWER('AABBCC')
---------------
aabbcc

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

9.RPAD和LPAD(粘贴字符) :
RPAD函数从右边对字符串使用指定的字符进行填充
SQL> select rpad('ren','5','a')from dual;

RPAD('REN','6','A')
-------------------
renaaa

LPAD 函数从左边对字符串使用指定的字符进行填充
SQL> select lpad('ren','6','a')from dual;
 
LPAD('REN','6','A')
-------------------
aaaren

10.LTRIM和RTRIM:
LTRIM 删除左边出现的字符串RTRIM 删除右边出现的字符串
SQL> select ltrim('ren he mu','ren') from dual;
 
LTRIM('RENHEMU','REN')
----------------------
 he mu
RTRIM 删除右边出现的字符串
SQL> select rtrim('ren he mu','mu') from dual;
 
RTRIM('RENHEMU','MU')
---------------------
ren he

11.SUBSTR(string,start,count)  取子字符串,从start开始,count数
SQL> select substr('aaccbb',2,4) from dual;
 
SUBSTR('AACCBB',2,4)
--------------------
accb

12.REPLACE替换(’string’,’s1’,’s2’) string 希望被替换的字符或变量
s1 被替换的字符串
s2 要替换的字符串
SQL> select replace('ren he mu','ren','aaa') from dual;
 
13.SOUNDEX 返回一个与指定字符串读音相同的字符串

select ename from emp where soundex(ename)=soundex('allen');

14.ABS 返回指定值的绝对值
SQL> select abs(100),abs(-100) from dual;
 
  ABS(100)  ABS(-100)
---------- ----------
       100        100

15.ACOS 给出反余弦的值
SQL> select acos(-1) from dual;
 
  ACOS(-1)
----------
3.14159265

16.ASIN 给出反正弦的值
SQL> select asin(0.5) from dual;
 
 ASIN(0.5)
----------
0.52359877

17.ATAN 返回一个数字的反正切值
SQL> select atan(0.5) from dual;
 
 ATAN(0.5)
----------
0.46364760

18.CEIL 返回大于或等于给出数字的最小整数
SQL> select ceil(5.69340) from dual;
 
CEIL(5.69340)
-------------
            6

19.COS 返回一个给定数字的余弦
SQL> select cos(5) from dual;
 
    COS(5)
----------
0.28366218

20.COSH 返回一个数字反余弦值
SQL> select cosh(10) from dual;
 
  COSH(10)
----------
11013.2329

21.EXP 返回一个数字x的n次方根
SQL> select exp(10) from dual;
 
   EXP(10)
----------
22026.4657

22.FLOOR  对给定的数字取整数
SQL> select floor(321.12) from dual;

23.LN  返回一个数字的对数值
SQL> select ln(1),ln(2),ln(2.7182818) from dual;

24.LOG(n1,n2) 返回一个以n1为底n2的对数
SQL> select log(2,1),log(2,4) from dual;


25.MOD(n1,n2) 返回一个n1除以n2的余数
SQL> select mod(10,3),mod(3,3),mod(2,3) from dual;

26.POWER 返回n1的n2次方根
SQL> select power(2,10),power(3,3) from dual;
POWER(2,10) POWER(3,3)
----------- ----------
1024 27

27.ROUND和TRUNC 按照指定的精度进行舍入
SQL> select round(55.5),round(-55.4),trunc(55.5),trunc(-55.5) from dual;
ROUND(55.5) ROUND(-55.4) TRUNC(55.5) TRUNC(-55.5)
----------- ------------ ----------- ------------
56 -55 55 -55

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

29.SIN 返回一个数字的正弦值
SQL> select sin(1.57079) from dual;
SIN(1.57079)
------------
1

30.SIGH 返回双曲正弦的值
SQL> select sin(20),sinh(20) from dual;
SIN(20) SINH(20)
--------- ---------
.91294525 242582598

31.SQRT 返回数字n的根
SQL> select sqrt(64),sqrt(10) from dual;
SQRT(64) SQRT(10)
--------- ---------
8 3.1622777

32.TAN 返回数字的正切值
SQL> select tan(20),tan(10) from dual;
TAN(20) TAN(10)
--------- ---------
2.2371609 .64836083

33.TANH 返回数字n的双曲正切值
SQL> select tanh(20),tan(20) from dual;
TANH(20) TAN(20)
--------- ---------
1 2.2371609

34.TRUNC 按照指定的精度截取一个数
SQL> select trunc(123.456,-2) from dual;
 
TRUNC(123.456,-2)
-----------------
              100

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

SQL> select to_char(add_months(to_date('199912','yyyy-mm'),-2),'yyyy-mm') from dual;

36.MONTHS_BETWEEN(date2,date1) ,给出date2-date1的月份
SQL> select months_between('19-12月-1999','19-3月-1999') mon_between from dual;

MON_BETWEEN
-----------
          9
SQL> select months_between(to_date('2000.05.20','yyyy.mm.dd'),to_date('2005.05.20','yyyy.mm.dd')) mon_betw from dual;

  MON_BETW
----------
       -60

37.SYSDATE 用来得到系统的当前日期
SQL> select to_char(sysdate,'dd-mm-yyyy day') from dual;
 
TO_CHAR(SYSDATE,DD-MM-YYYYDAY
------------------------------
23-02-2012 星期四
09-05-2004 星期日
trunc(date,fmt)按照给出的要求将日期截断,如果fmt=’mi’表示保留分,截断秒
SQL>  select to_char(trunc(sysdate,'hh'),'yyyy.mm.dd hh24:mi:ss') hh, to_char(trunc(sysdate,'mi'),'yyyy.mm.dd hh24:mi:ss') hhmm from dual;

38.CHARTOROWID 将字符数据类型转换为ROWID类型

SQL> select rowid,rowidtochar(rowid),ename from emp;

ROWID              ROWIDTOCHAR(ROWID) ENAME
------------------ ------------------ ----------
AAAMfMAAEAAAAAgAAA AAAMfMAAEAAAAAgAAA SMITH
AAAMfMAAEAAAAAgAAB AAAMfMAAEAAAAAgAAB ALLEN
AAAMfMAAEAAAAAgAAC AAAMfMAAEAAAAAgAAC WARD
AAAMfMAAEAAAAAgAAD AAAMfMAAEAAAAAgAAD JONES

39.TO_CHAR(date,’format’)
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
 
TO_CHAR(SYSDATE,YYYY-MM-DDHH2
------------------------------
2012-02-23 23:48:53

40.TO_DATE 将字符串转化为ORACLE中的一个日期
select ename,hiredate from emp where hiredate=to_date('19810222','yyyymmdd');

41.TO_MULTI_BYTE 将字符串中的单字节字符转化为多字节字符
SQL> select to_multi_byte('aaa') from dual;
 
TO_MULTI_BYTE('AAA')
--------------------
aaa

41.TO_NUMBER 将给出的字符转换为数字
select to_number('$123,233,455,623.3400','$999,999,999,999.0000') from dual;

42.DUMP(s,fmt,start,length) DUMP函数以fmt指定的内部数字格式返回一个VARCHAR2类型的值
SQL> col global_name for a30
SQL> col dump_string for a50
SQL> set lin 200
SQL> select global_name,dump(global_name,1017,8,5) dump_string from global_name;
GLOBAL_NAME DUMP_STRING
------------------------------ --------------------------------------------------
ORACLE.WORLD Typ=1 Len=12 CharacterSet=ZHS16GBK: W,O,R,L,D

43.EMPTY_BLOB()和EMPTY_CLOB() 这两个函数都是用来对大数据类型字段进行初始化操作的函数

44.GREATEST 返回一组表达式中的最大值,即比较字符的编码大小.
SQL> select greatest('AA','AB','AC') from dual;

SQL> select greatest('啊','安','天') from dual;

45.LEAST 返回一组表达式中的最小值
SQL> select least('啊','安','天') from dual;

46.UID 返回标识当前用户的唯一整数
SQL> select username,user_id from dba_users where user_id=uid;

47.USER 返回当前用户的名字
SQL> select user from dual;

48.USEREVN 返回当前用户环境的信息,option可以是:
ENTRYID,SESSIONID,TERMINAL,ISDBA,LABLE,LANGUAGE,CLIENT_INFO,LANG,VSIZE
ISDBA 查看当前用户是否是DBA如果是则返回true
SQL> select userenv('isdba') from dual;

SESSION  返回会话标志
SQL> select userenv('sessionid') from dual;

ENTRYID 返回会话人口标志
SQL> select userenv('entryid') from dual;

INSTANCE 返回当前INSTANCE的标志
SQL> select userenv('instance') from dual;

language 返回当前环境变量
SQL> select userenv('language') from dual;

LANG 返回当前环境的语言的缩写
SQL> select userenv'lang') from dual;

TERMINAL 返回用户的终端或机器的标志
SQL> select userenv('terminal') from dual;
 
49.AVG(DISTINCT|ALL) all表示对所有的值求平均值,distinct只对不同的值求平均值

SQL> select avg(distinct sal) from emp;

SQL> select avg(sal) from emp;

50.MAX 求最大值
SQL> select max(sal) from emp;

51.MIN 求最小值
SQL> select min(sal) from emp;


52.STDDEV(distinct|all) 求标准差,ALL表示对所有的值求标准差,DISTINCT表示只对不同的值求标准差
SQL> select stddev(sal) from emp;

SQL> select stddev(distinct sal) from emp;

53.VARIANCE(DISTINCT|ALL) 求协方差
SQL> select variance(sal) from emp;

54.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
统计每个部门的员工人数,薪金总数

55.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
以上两种方式都可以,having条件对部门员工数大于等于5的输出

56.ORDER BY 用于对查询到的结果进行排序输出

SQL> select sal from emp order by sal desc;

默认是升序,desc降序