Oracle常用函数
字符函数:
1、INSTR(C1,C2,I,J)
C1 被搜索的字符串
C2 希望搜索的字符串
I 搜索的开始位置,默认为1
J 出现的位置,默认为1
SQL> select instr(oracle traning,ra,1,2) instring from dual;
2、LOWER和UPPER
返回字符串,并将所有的字符小写
SQL> select lower(AaBbCcDd) from dual;
3、SUBSTR(string, start, count)
取子字符串,从start开始,取count个
SQL> select substr(13088888888,3,8) from dual;
4、REPLACE(string,s1,s2)
string 希望被替换的字符或变量
s1 被替换的字符串
s2 要替换的字符串
SQL> select replace(he love you,he,i) from dual;
5、TRIM(s from string)
LEADING 剪掉前面的字符
TRAILING 剪掉后面的字符
如果不指定,默认为空格符。
数学函数:
6、ROUND和TRUNC
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
还有其他 正弦 余弦 平方等等数学函数。
时间函数:
7、ADD_MONTHS
增加或减去月份
SQL> select to_char(add_months(to_date('199912','yyyymm'),-2),'yyyymm') from dual;
--- 199910
8、LAST_DAY
返回日期的最后一天
SQL> select last_day(sysdate) from dual;
--- 31-5月 -04
9、MONTHS_BETWEEN(date2,date1)
给出date2至date1的月份
SQL> select months_between('19-12月-1999','19-3月-1999') mon_between from dual;
--- 9
转换函数:
10、TO_CHAR(date,'format')
SQL> select to_char(sysdate,'yyyy/mm/dd hh24:mi:ss') from dual;
---2015/05/15 11:14:32
11、TO_DATE(string,'format')
将字符串转化为ORACLE中的一个日期
SQL> select to_date('2015/05/15 11:14:32','yyyy/mm/dd hh24:mi:ss') from dual;
---2015/05/15 11:14:32
12、TO_NUMBER
将给出的字符转换为数字
SQL> select to_number('1999') year from dual;
---1999
聚合函数:
13、AVG(DISTINCT|ALL)
all表示对所有的值求平均值,distinct只对不同的值求平均值
SQL> select avg(distinct sal) from .table.column; --对所有的值求平均值
SQL> select avg(all sal) from table.column; --先去重,再求平均值
14、MAX(DISTINCT|ALL) 和 MIN(DISTINCT|ALL)
求最大值和最小值,ALL表示对所有的值求最大值,DISTINCT表示对不同的值求最大值,相同的只取一次。
SQL> select max(distinct sal) from table.column;
SQL> select min(all sal) from table.column;
15、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
16、HAVING是GROUP BY分组的条件
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
17、ORDER BY 排序 desc和asc
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
18、NVL(null,0)替代
19、distinct 去重
20、count(*)总数