oracle--函数

一、单行函数(常用的)

1.字符函数

--dual是伪表

--求字符串长度

select length('ABCD')from dual

求字符串子串(原字符串,从第几位截取,截取字符数)

select substr('abcd',2,2)from dual

--字符串拼接,一次只能拼接一次

select concat('abc','d') from dual

--可以用||拼接,可拼接多个

select 'abc'||'D'from dual

二、数值函数

--四舍五入

select round (100.56,2)from dual

--数字截取

select trunc(100.567,2) from dual

--取模

select mod(10,3)from dual

三、日期函数

--时间函数

select sysdate hz from dual

--加月函数

select add_months(sysdate,2)from dual

--求所在月的最后一天

select last_day(sysdate) from dual

--日期截取,trunc

按日截取

select trunc(sysdate) from dual

按月截取

select trunc(sysdate,'mm') from dual

按年截取

select trunc(sysdate,'yyyy') from dual

小时就hh,分钟就mi

四、转换函数

--数字转字符串

select to_char(100) from dual;

--拼接字符串也可以转为字符串

select 100 || '分' from dual;

--日期转字符串

select to char(sysdate,'yyyy-mm-dd')

--字符串转日期

select to_date('2026-03-10','yyyy-mm-dd')from dual

--字符串转数值

select to_number(1100) from dual

五、其他函数

--空值处理函数null

select nvl(null,0)from dual -- 第一个参数为null就返回后面第二个参数0,否则返回第一个参数

查询ownertypeid =1的t_pricetable,如果maxnum为null就变成999

select nvl(maxnum,999) from t_pricetable where ownertypeid =1

--nvl2有三个参数,第一个检测是否为null,是null就返回第三个,不是就返回第二个

select * from t_pricetable where ownertypeid =1

select nvl2(maxnum,to_char(999),'为空') from t_pricetable where ownertypeid =1

--条件判断decode

--语法:decode(条件,值1,翻译值1,值2,翻译值2,....,缺省值) 根据条件返回相应的值,都不符合就是缺省值,缺省值可以省略

select decode(400,1,2,3,4,100,200,300) from dual -- 返回的是300

查业主表,1就是居民,

select * from t_owners

select name,decode(ownertypeid,1,'居民',2,'行政','其他')from t_owners

--case when then

select name,(

case

when ownertypeid=1 then '居民'

when ownertypeid=2 then '行政'

when ownertypeid=3 then '其他'

else '不存在'

end

) from t_owners

--行列转换

按月份统计2012,1月到4月各地区的水费

select * from t_account

select t_area.name,

sum(case when month = '01' then money else 0 end ),

sum(case when month = '02' then money else 0 end ),

sum(case when month = '03' then money else 0 end ),

sum(case when month = '04' then money else 0 end )

from t_account left join t_area on t_area.id = t_account.areaid

where t_account.year = '2012'

group by t_account.areaid,t_area.name

六、窗口函数(分析函数)

--排名函数必须搭配over函数

--over()就是用来指定,在什么数据里,按什么顺序,做求和/计算//排名,但不减少行

值相同,排名相同,序号跳跃 rank()

select rank() over(order by usenum desc) 排名, t.* from t_account t

值相同,排名相同,序号连续 dense_rank()

select dense_rank() over(order by usenum desc) 排名, t.* from t_account t

序号连续,不管值是否相同 row_number()

select row_number() over(order by usenum desc) 排名, t.* from t_account t

用分析函数实现分页

select * from

(select row_number() over (order by usenum desc) rownumber,t.* from t_account t)

where rownumber >=10 and rownumber < =20

七、集合运算

--将两个或多个结果集合成为一个结果集,集合运算

--并集(包括重复的记录)union all

select * from t_owners where id >5

union all

select * from t_owners where id <8

--并集(去掉重复的记录)union

select * from t_owners where id >5

union

select * from t_owners where id <8

--交集(两个结果集的重复部分)intersect

select * from t_owners where id >5

intersect

select * from t_owners where id <8

--差集minus

select * from t_owners where id >5

minus

select * from t_owners where id <8

select * from t_owners where id <8

minus

select * from t_owners where id <5

差集的分页查询

select rownum,t.* from t_account t where rownum <=20

minus

select rownum,t.* from t_account t where rownum <=10

相关推荐
摇滚侠3 小时前
expdp 查看帮助
java·数据库·oracle
流年似水~3 小时前
MCP协议实战:从零搭建一个让Claude能“看见“数据库的工具服务
数据库·人工智能·程序人生·ai·ai编程
2401_871492853 小时前
Vue.js监听器watch利用回调函数处理级联下拉框数据联动
jvm·数据库·python
志栋智能4 小时前
超自动化安全:构建智能安全运营的核心引擎
大数据·运维·服务器·数据库·安全·自动化·产品运营
zhoutongsheng5 小时前
C#怎么实现Swagger文档 C#如何在ASP.NET Core中集成Swagger自动生成API文档【框架】
jvm·数据库·python
WinterKay5 小时前
【开源】我写了一个轻量级本地数据库浏览工具,支持 MySQL/Redis 只读查询
数据库·mysql·开源
zxrhhm6 小时前
Oracle 索引完整指南
数据库·oracle
程序猿乐锅7 小时前
【Tilas|第三篇】多表SQL语句
数据库·经验分享·笔记·学习·mysql
Navicat中国8 小时前
使用 Navicat 导入向导导入 Excel 数据时,系统提示导入成功,表中也能看到数据,但行数统计显示为 0,这是什么原因?
数据库·excel·导入
gmaajt8 小时前
Golang怎么做国际化多语言_Golang i18n教程【核心】
jvm·数据库·python