---
title: "Oracle中常用的函数总结"
author: "slbszq"
date: "2014-01-14T11:47:42Z"
tags: ["Oracle", "函数", "常用", "总结"]
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2014-01-14T11:47:42Z"
id: 2003715
url: "https://it4j.iteye.com/blog/2003715"
---

**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)**不进行四舍五入,**直接截取**

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** 注：Oracle中**1个汉字算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)) &gt; 10000;