Oracle中常用的函数总结

 Oracle中常用的函数

--单行函数

--数字函数

--1. round 四舍五入 a. round(m)

select round(1.4), round(1.5) from dual;                                                       

--         1          2                                                          

--b. round(m,n)

select round(15.193, 2), round(15.193, -1) from dual;                                             

--          15.19               20                                               

 

--2. trunc(m, n)不进行四舍五入,直接截取

扫描二维码关注公众号,回复: 1241515 查看本文章

select trunc(15.79, 1), trunc(15.79, -1) from dual;                                              

--          15.7              10                                                 

 

--3. mod(m, n)注意:当m, n为负数时,结果与数学上的经典求模结果不一样

select mod(11, 4) from dual;                                                                 

--         3                                                                     

--Oracle求模与数学上经典求模差别

m

n

MOD(m, n)

Classical Modulus

11

4

3

3

11

-4

3

-1

-11

4

-3

1

-11

-4

-3

-3

 

--4. ceil(天花板)返回不小于参数的最小值

select ceil(1.5), ceil(1.2) from dual;                                                       

--         2          2                                                          

 

--5. floor(地板)返回不大于该参数的最大值

select floor(1.9), floor(1.2), floor(-1.9) from dual;                                         

--         1          1          -2                                              

 

--字符函数

--1. upper 把字符串中的每个单词字母转换成大写

select upper('Zhengzhou') from dual;                                                                   

--ZHENGZHOU                                                                       

 

--2. lower 把字符串中的每个单词字母转换成小写

select lower('ZhengZHOU') from dual;                                                    

--zhengzhou                                                                      

 

--3. initcap 把字符串中单词首字母转换成大写

select initcap('zhong hua ren min gong he guo') from dual;                                                 

--Zhong Hua Ren Min Gong He Guo                                                  

 

--4. ltrime/rtrim/trim裁剪掉前导/后导/两端空格字符

select length(ltrim(' aa  ')), length(rtrim(' aa  ')), length(trim(' aa  ')) from dual;                

--                  4                   3                  2                     

 

-- leading 代表前导, trailing代表后导, both代表全部

select trim(leading 'a' from 'aabbccaa') "leading",

       trim(trailing 'a' from 'aabbccaa') "traling",

       trim(both 'a' from 'aabbccaa') "both" from dual;                                                         

 

--bbccaa aabbcc bbcc                                                              

 

--5. substr 截取字符串

--a. 从字符串左边开始的3字符开始截取,长度为4

select substr('abcdefg', 3, 4) from dual;                                                                  

--cdef                                                                           

 

--b. 从字符串右边开始的5字符开始截取,长度为4(小数忽略)

select substr('abcdefg', -5, 4) from dual;                                                                       

--cdef                                                                           

 

--c. 从字符串左边开始的第二个字符开始截取,长度为4(小数忽略)

select substr('abcdefg', 2, 4.3) from dual;                                                                      

--bcde                                                                           

 

--d. 从第二个字符开始截取,长度超出范围只会输出剩余字符.

select substr('abcdefg', 2, 8.8) from dual;                                                                       

--bcdefg                                                                         

 

--6. length 注:Oracle1个汉字算1个字符,也是使用unicode编码

select length('zhongguo'), length('中国') from dual;                                    

--                 8              2                                              

 

-- lengthb 得到字符串的字节长度

select lengthb('zhongguo'), lengthb('中国') from dual;                                         

--                  8               4                                            

 

--7. replace(src, search, replacement) 把字符串中指定的字符删除或者替换成别的字符

select replace('chen', 'c'), replace('chen', 'c', 'aa') from dual;                                               

--hen aahen                                                                       

 

--8. concat(char1, char2)拼接字符串 注:所有SQL,+只用于数字

select concat('中华', '人民') || '共和国' || 1949 from dual; 

--中华人民共和国1949                                                             

 

--日期函数 Oracle, 有如下操作时合法的:

--日期-日期 = 数字 (相差的天数)

--日期+数字 = 新的日期 (数字指添加的天数)

--1. months_between(date1, date2)算出两个日期间的间隔月数

select months_between(

       to_date('2008-08-08', 'yyyy-mm-dd'),

       to_date('2013-08-08', 'yyyy-mm-dd')

) from dual;                                                                   

--       -60                                                                     

 

--next_day(date, char) 返回当前日期的下一个字符串参数指定的日期char必需表示一周中的某一天

select next_day(to_date('2014-01-13', 'yyyy-mm-dd'), '星期三') from dual;

--2014-01-15                                                                      

 

--3. last_day(date) 得到某个日期当月最后一天的日期

select last_day(to_date('2014-01-13', 'yyyy-mm-dd')) from dual;

--2014-01-31                                                                     

 

--4. round 日期四舍五入

select round(to_date('2014-01-13', 'yyyy-mm-dd'), 'year'),

round(to_date('2014-07-01', 'yyyy-mm-dd'), 'year') from dual;

--2014-01-01 2015-01-01                                                          

 

--转换函数

--1. to_number(expr, fmt) 把字符串按指定格式转换成整数

select to_number('123,456,789', '999,999,999') from dual;                                     

--                             123456789                                         

 

--2. to_char(number) 字符串按照指定格式转换

--a. $ 代表美元符号

select ename, to_char(sal, '$999,999,999,99') from emp;

 

--b. L 代表系统本地化的货币格式

select ename, to_char(sal, 'L999,999,999,99') from emp;

 

--c. to_char(datetime, fmt) 把一个日期时间转换成指定的字符串格式

select ename, to_char(hiredate, 'yyyy-mm-dd hh24:mi:ss') from emp;

 

-- 注意:除了,./等少数标点外,日期格式中其他字符必须用“”括起来

--3. to_date 将指定文本格式的日期转换为日期类型.

select ename, job, sal, hiredate from emp where hiredate = to_date('1981-5-1', 'yyyy-mm-dd');

 

-- 其它函数

-- 1. nvl(exp1, exp2) 用于处理null,如果exp1的值为空则表达式值为exp2,否则为exp1.

select ename, (sal + nvl(comm, 0))*12 from emp;

 

-- 2. decode执行类似于switch.. case.. default的选择逻辑

-- 将岗位中的ANALYST显示为分析师,SALESMAN显示为销售,

-- MANAGER显示为经理,其余的显示为其他

select ename, job, decode(job,

       'ANALYST', '分析师',

       'SALESMAN', '销售员',

       'MANAGER', '经理',

'其他') from emp;

 

-- 多行函数/聚合函数/分组函数

-- 多行函数:一次调用,处理多行数据

-- 1. avg:求多行一列的列平均值

select avg(sal) from emp where deptno = 10;

-- 注:分组查询时,select子句中只能出现分组表达式和分组函数

-- 除此之外的任何列都是非法的。

select deptno, avg(sal) from emp group by deptno;

 

-- 2. count 统计数量count忽略null.查询显示奖金的人数

select count(comm) from emp;

 

-- 3. max:返回多行一列中最大行的值

select max(sal) from emp;

 

-- 4. min:返回多行一列中最小行的值

select min(sal) from emp;

 

-- 5. sum:返回多行一列的和

-- 结论:where子句中禁止出现聚合函数!如需过滤聚合函数的结果,

-- 则使用having子句,如:显示部门开支超过一万的。

select deptno, sum(sal + nvl(comm, 0)) from emp

group by deptno having sum(sal + nvl(comm, 0)) > 10000;

猜你喜欢

转载自it4j.iteye.com/blog/2003715