您的位置:首页 > 数据库 > Oracle

Oracle函数

2016-03-28 17:21 621 查看
一.单行函数

1.大小写控制

lower 转小写

upper 转大写

initcap 首字母大写

select lower('Hello World'), upper('hello world'),initcap('hello world') from dual;


2.字符控制

concat 连接符

substr截取字符串 substr(a,b,c) 从a中,第b位开始取,取c位

length 字符数/lengthb 字节数(一个汉字包含2个字节)

instr instr(a,b) 在a中,查找b 查找字符所在的位置

lpad左填充/rpad 右填充 lpad/rpad(‘需要填充的字符串’,字符串总个数,’用来填充的字符’)

trim去掉前后指定的字符trim(‘需要去除的字符’ from ‘目标字符串’)

replace 替换字符replace(‘目标字符串’,’需要替换掉的字符’,’用来替换的字符’)

--大小写函数:
select lower('SMITH') from dual     SMITH->smith
select upper('smith') from dual         smith->SMITH
select initcap('smith') from dual       smith->Smith
--字符串操作函数:
--字符串拼接函数:
select concat('AS','WD') from dual

--字符串长度函数:
select length('张三') from dual(按字)2
select lengthb('张三') from dual (按字节)4
select lengthc('张三') from dual (unicode的长度) 2

substr('字符串'm,n) m是从第几个字符串开始,如果为负则从后边的第几个开始;
n是数多少个,如果省略则到字符串的结尾
select 'smith',substr('smith',1,2) first,substr('smith',-1) last from dual

--获取子串在父串中的位置:0则表示没有在父串中找到改子串
select instr('smith','i') from dual;

--截取字符串:
select trim(leading 'a' from 'aaabbbaabababaaaa') from dual;    截取连续的前置的a
select trim(trailing 'a' from 'aaabbbaabababaaaa') from dual;   截取连续的后置的a
select trim(both 'a' from 'aaabbbaabababaaaa') from dual;       截取连续的前置和后置的a
select trim('a' from 'aaabbbaabababaaaa') from dual;            如果不说明是leading/trailing 则执行both


3.数字函数

round() 四舍五入

trunc() 截取

mod() 取余

select round(55.926,2) 一,round(55.926,1) 二,round(55.926,0) 三,round(55.926,-1) 四,round(55.926,-2) 五 from dual;//55.93    55.9    56  60  100
select trunc(55.926,2) 一,trunc(55.926,1) 二,trunc(55.926,0) 三,trunc(55.926,-1) 四,trunc(55.926,-2) 五 from dual;//55.92    55.9    55  50  0

--返回输入参数的集合(N1,N2,N3,第二等)的最大值。
select greatest(1,null) from dual;
select greatest(3,5,1,8,33,99,34,55,67,43) as maxInt from dual;


4.日期函数

查询系统日期

格式化日期

日期的+/-

months_between 两个日期之间相差的月数

add_months 向指定日期上加上若干个月

next_day 指定日期的下一个日期

last_day 本月的最后一天

round 日期的四舍五入

trunc 日期的截取

select ename,hiredate,round((sysdate-hiredate),2) 天 from emp;
select hiredate+sysdate from emp;--不允许日期 + 日期
select ename,hiredate,round((sysdate-hiredate)/30,2) 一,round(months_between(sysdate,hiredate),2) 二 from emp;
select add_months(sysdate,7) from dual;
select last_day(sysdate) from dual;
select next_day(sysdate,'星期三') from dual;
select round(sysdate,'month'),round(sysdate,'year') from dual;--日期的四舍五入,日期、月份过半则进一
select trunc(sysdate,'month'),trunc(sysdate,'year') from dual;--日期的截取


5.转换函数



select to_char(sysdate,'yyyy-mm-dd hh:mi:ss "今天是:"day') from dual;--2016-03-29 09:36:47 今天是:星期二
--查询员工薪水:两位小数,千位符,货币代码
select to_char(sal,'L9,999.99') from emp;


6.通用函数-适用于任何数据类型/null

nvl(a,b) 当啊为null时,返回b

nvl2(a,b,c) 当a为null返回c 否则返回b

nullif(a,b) 当a=b,返回空值,否则返回a

coalesce(a,b,c,d….)从左到右,找到第一个不为null的值

--nvl2(a,b,c) 当a=null时候,返回c;否则返回b
select ename,sal, sal*12+nvl2(comm,comm,0)年薪 from emp;
--nullif(a,b) 当a=b时候,返回null,否则返回a
select nullif('abc','abcd') from dual;
--coalesce  从左到右 找到第一个不为null的值
select ename,comm,sal,coalesce(comm,sal)"第一个不为null的值" from emp;


7.条件表达式

在sql语句中使用IF-THEN-ELSE逻辑

CASE表达式:SQL99语法

DECODE函数:Oracle自己的语法

--CASE 表达式 when 条件1 then 返回值1
--           when 条件2 then 返回值2
--END
select ename,job,sal 涨前,case job when upper('president') then sal+1000
when upper('manger') then sal+800
else sal+400
end 涨后
from emp;
--DECODE(表达式,条件1,返回值1,
--             条件2,返回值2,
--             条件3,返回值3...  )
select ename,job,sal 涨前,decode(job,'PRESIDENT',sal+1000,
'MANGER',sal+800,
sal+400) 涨后
from emp;


8.Oracle函数-操作数据为null的函数

nvl(expr1,expr2)            如果expr1不为空 返回 expr1 否则返回expr2 两个表达式的数据类型一定要相同

nvl2(expr1,expr2,expr3)     如果expr1不为空 返回 expr2 否则返回expr3

nullif(expr1,expr2)         如果expr1与expr2相同就返回空,否则返回expr1

coalesce(expr1,expr2,expr3.....) 返回括号内第一个非空的值


二:多行函数/组函数/分组函数

AVG() 平均值

COUNT() 计数 count(distinct expr) 返回expr非空且不重复的记录

MAX() 最大值

MIN() 最小值

SUM() 合计

GROUP BY()

HAVING子句

1.GROUP BY()

--在select列表中所有未包含在组函数中的列都应包含在group by 子句中
select deptno,AVG(sal) from emp group by deptno;
--包含在group by 子句中的列可以不包含在select列表中
select avg(sal) from emp group by deptno;
--多个列分组
select deptno,job,sum(sal) from emp group by deptno,job order by deptno;




group by增强版

break on deptno skip 2;
select deptno,job,sum(sal) from emp group by rollup(deptno,job);
break on null;

select deptno,job,sum(sal) from emp group by rollup(deptno,job);
--效果等同于
select deptno,job,sum(sal) from emp group by deptno,job;
+
select deptno,sum(sal) from emp group by deptno;
+
select sum(sal) from emp;




group by rollup(a,b)
=group by a,b
+group by a
+group by null


2.HAVING子句-过滤分组

行已经被分组

使用了组函数

满足having子句中条件的分组将被显示

having与where的区别:

where 不能使用组函数

where是先过滤后查询 having是先查询后过滤速度慢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle