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

关于oracle数据库(10)函数

2016-03-29 19:05 441 查看
分析函数,用于统计排名

语法:函数名() over(order by 排序字段 asc | desc)

row_number() 无论值是否相等,生成连续的行号

-- 1,2,3,4,

select ename,sal,row_number() over (order by sal desc) 名次 from emp;

dense_rank() 如果值相等,则排名相同,排名仍连续

--1,2,2,3,4,5

select ename,sal,dense_rank() over (order by sal desc) 名次 from emp;

rank() 如果值相等,则排名相同,排名不连续

select ename,sal,rank() over (order by sal desc) 名次 from emp;

select ,case when 条件 then '结果' else '' ,from 表

select ename,sal, case

when sal<=1000 then '★'

when sal<=2000 then '★★'

when sal<=3000 then '★★★'

when sal<=4000 then '★★★★'

when sal<=5000 then '★★★★★'

end 等级 from emp;

单行函数(标量函数),1行数据返回1个结果

日期函数

sysdate 返回系统当前时间

--dual 专业用来做运算的表 只有一行一列

select sysdate from dual;

select 1+2+3+4*5*5/7 from dual;

用于加/减,关系运算

select sysdate 当前时间 from dual

--几天之后

select sysdate+3 from dual

--几天之前

select sysdate-3 from dual

--间隔天数

距离年底还有多少天

select to_date('2016-12-31','yyyy-mm-dd')-sysdate from dual

--查询1982年1月1日之后入职的员工

select * from emp

where hiredate>to_date('19820101','yyyymmdd');

months_between(d1,d2) 返回指定日期之间的月份间隔

add_months(d,num) 返回给指定日期加上整数个月后的新日期

--查询入职时间和转正时间 [3个月转正]

last_day(d) 返回指定日期当月的最后1天

--查询每月最后1天入职的员工

--查询每月倒数第3天入职的员工

字符函数:字符串下标从1开始,0也表示第1位

length(str) 返回字符串的长度

select length('abc123456') from dual;

select ename,length(ename) from emp;

substr(str,index)/substr(str,index,length)

select substr('huwen@163.com',6) from dual; --下标从1开始

--java 下标0开始 oracle 下标1 开始

select substr('huwen@163.com',7,3) from dual;

--java 第1才是 下标,第2个参数还是下标

--oracle 第1个是下标 第2个长度

instr(str1,str2) 返回str1中str2出现的下标

--instr就相当于java indexOf()

select instr('yyyy@163.com','@') from dual;

select '&name' from dual

--'&xxx' 表示接收用户输入

select substr('&email',1,instr('&email','@')-1) from dual;

select substr('&email',instr('&email','@')+1) from dual;

转换函数

to_char(d)/to_char(d,fmt) 日期>>字符

--转默认格式的字符串

select to_char(sysdate) from dual;

select to_char(sysdate,'yyyy"年"mm"月"yy"日"') from dual

--转指定格式的字符串:yyyy,mm,dd,hh/hh24,mi,ss,day

select to_char(sysdate,'day') from dual;

select to_char(sysdate,'yyyy/mm/dd') from dual;

select to_char(sysdate,'hh24:mi:ss') from dual;

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss day') from dual;

select to_char(sysdate,'fmyyyy"年"mm"月"dd"日" day') from dual;

--fm 表示去掉日期前缀0

select hiredate ,to_char(hiredate,'yyyy') from emp;

to_date(str)/to_date(str,fmt) 字符>>日期

--默认格式

select to_date('1-10月-12')-sysdate from dual;

--指定格式

select to_date('2012-10-1','yyyy-mm-dd')-sysdate from dual;

select to_date('2012-10-1 15:50:00','yyyy-mm-dd hh24:mi:ss')-sysdate from dual;

to_char(num,fmt) 数字>>字符,用于货币的显示

select to_char(10000,'9,999,999.99') from dual;

select ename,sal,

to_char(sal,'$9,999.99') 美元,

to_char(sal,'C9,999.99') 人民币,

to_char(sal,'L9,999.99') 本土 from emp;

数学函数

mod(n1,n2) 返回n1除以n2的余数

select mod(7,2) from dual -- 7%2

ceil(num) 返回大于等于该数的最小整数--向上取整

select ceil(4.0001) from dual

floor(num) 返回小于等于该数的最大整数--向下取整

select floor(4.99999) from dual

round(num)/round(num,s) 四舍五入,s表示小数位数

select round(4.5) from dual;

select round(4.4) from dual;

select round(-11.5) from dual; --java里面是-11 这里-12

--保留3位小数

select round(3.141596,3) from dual;

trunc(num)/trunc(num,s) 截断数字,直接舍去

select trunc(4.7) from dual;

select trunc(4.79,1) from dual; --保留一位小数

其他函数

nvl(exp1,exp2) 空值替换函数

含义:如果表达式exp1为null,则使用表达式exp2代替

--Oracle排序时,将空值null理解为最大

select ename,sal,comm from emp order by comm desc

select * from emp order by nvl(comm,0) desc,sal desc;

--表达式计算时,如果包含空值null,则返回null

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

decode() 相当于case..when..语句

case when 条件 then 结果 -- 相当于java if else if

decode 就相当于switch 可以判断相等

select ename,deptno,

decode(deptno,10,'技术部',20,'销售部',30,'管理层')

from emp;

select ename,deptno,

decode(deptno,10,'AAA',20,'BBB',30,'CCC') 部门 from emp;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: