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

相关推荐
GBASE18 小时前
G术时刻 |GBase 8s数据库事务并发控制之封锁技术介绍(下)
数据库
xiezhr1 天前
逛GitHub发现了一款免费的带AI功能的数据库管理工具
数据库·ai编程·dba
唐青枫2 天前
MySQL JSON 实战详解:从存储、查询、更新到 JSON_TABLE 与索引
sql·mysql
吃糖的小孩2 天前
给 QQ AI 机器人设计“可控记忆”:会话摘要、手动长期记忆与角色卡边界
数据库
小满8782 天前
5.Mysql事务隔离级别与锁机制
mysql
笃行3503 天前
金仓数据库数据安全双防线:静态存储加密与传输加密实战
数据库
笃行3503 天前
金仓数据库物理备份实战:sys_rman 全流程演练与误覆盖抢救
数据库
笃行3503 天前
金仓数据库逻辑备份实战:从全库导出到 Schema 替换的完整闭环
数据库
元Y亨H3 天前
技术笔记:MySQL 字符集排序规则与大小写敏感性问题解决方案
mysql