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

相关推荐
Leon-Ning Liu2 小时前
Oracle 26ai 新特性: True Cache(真实缓存)
数据库·缓存·oracle
Leon-Ning Liu2 小时前
Oracle 26ai 的 SQL 语言增强特性
数据库·sql·oracle
Elastic 中国社区官方博客2 小时前
Elasticsearch:语义搜索,现在默认支持多语言
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
小江的记录本2 小时前
【JEECG Boot】 JEECG Boot 数据字典管理——六大核心功能(内含:《JEECG Boot 数据字典开发速查清单》)
java·前端·数据库·spring boot·后端·spring·mybatis
小年糕是糕手2 小时前
【35天从0开始备战蓝桥杯 -- Day9】
数据结构·数据库·c++·算法·蓝桥杯
weixin_307779132 小时前
使用COPY INTO从S3导入CSV文件到Azure Synapse Dedicated SQL Pool表的问题分析与自动化验证方案
sql·自动化·azure
Austindatabases2 小时前
SQLite需要初始化参数,怎么调优-- SQLite 五脏俱全系列 (1)
数据库·sqlite
NineData2 小时前
NineData V5.0 产品发布会:让 AI 成为数据管理的驱动力,4 月 16 日!
数据库·人工智能·数据库管理工具·ninedata·数据库迁移工具·数据安全管理·权限管控
漫霂2 小时前
SpringSecurity入门应用
java·数据库·spring