当前位置:网站首页>oracle数据库常用的函数总结
oracle数据库常用的函数总结
2022-06-25 13:44:00 【薄荷味脑花】
oracle数据库常用的函数总结
1.前言
在日常开发中,有些企业使用oracle数据库作为项目的数据存储容器,我就总结哈自己遇到的一些函数的用法。
2.总结的函数
2.1extract
extract:
oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分
用法:
select extract (year from sysdate) year, extract (month from sysdate) month, extract (day from sysdate) day from dual;
select extract (year from date '2015-05-04') year, extract (month from date'2015-05-04') month, extract (day from date '2011-05-04') day from dual;
extract(year from systimestamp) year,extract(month from systimestamp) month,extract(day from systimestamp) day
select extract(year from interval '21' year) year from dual
2.2sign
sign:
sign函数是根据给的数为正数,就返回1,0返回0,负数返回-1,我感觉这个函数用的蛮多的。需要注意sign(这个括号里面只能是个字段),我测试了,在括号中写个子查询直接就报错了
用法:
//需求:查物品,需要区分该物品的盈利情况,比如 goods_interest字段,有0.23,0,-0.23等
select sign(goods_interest)as interest_flag from goods
//效果是:1,0,-1
2.3decode
decode函数:就是给某个值取一个值,我感觉他非常好用,感觉很像java中的switch的用法,在字段存char类型数字表示不同汉字含义,通过这个函数,查出来,前端可以直接显示,不需要再转一道汉字,后端的导出也是一步到位,不需要再使用switch转汉字,简化代码。
用法:
decode(条件,值1,返回值1,值2,返回值2,…值n,返回值n,缺省值)
select
decode(PROBLEM_LEVEL,1,'一般',2,'较大',3,'重大')as PROBLEM_LEVEL
from accident
//效果:
如果PROBLEM_LEVEL是2,查出来就是较大
2.4to_date和to_char
to_date和to_char:因为这两个函数使用频率太高,放一起解释;to_date:是将oracle中时间样式的字符串或时间类型数据格式化为时间类型的数据(yyyy-MM-dd,yyyy-MM等等不同的时间样式),to_char是将当前值变成字符串类型,在时间处理方面,可以将时间类型的转化为字符串匹配,比如数据库存的时间类型,但后台接收的是一个字符串,格式是yyyy-MM,那么可以转字符串比,也可以转时间比,但注意只比较年月,其他内容截取
案例:
to_date(a.CHECK_TIME,"yyyy-MM-dd")>=to_date(#{param.startTime},"yyyy-MM-dd")
to_char(ss.P_TIME,'yyyy-mm-dd') >= to_char(#{param.startTime},'yyyy-mm-dd')
对to_char 和 to_date函数的补充:
测试代码:
select time from (select sysdate as time from dual)
select to_date(time) from (select sysdate as time from dual)
select to_date(time,'yyyy-MM-dd hh:mm:ss')from (select sysdate as time from dual)
select to_date(time,'yyyy-MM-dd hh:mi:ss')from (select sysdate as time from dual)
select to_char(time,'yyyy-MM-dd hh:mi:ss')from (select sysdate as time from dual)
select to_char(time,'yyyy-MM-dd')from (select sysdate as time from dual)
select to_char(time,'yyyy-MM')from (select sysdate as time from dual)
select to_date('2022-6-25','yyyy-MM-dd') from (select sysdate as time from dual)
select to_char('2022-6-25','yyyy-MM-dd')from (select sysdate as time from dual)
select to_date('2022-6-25') from (select sysdate as time from dual)
select to_char(time)from (select sysdate as time from dual)
按系统时间来举例:
当to_date(时间类型的数据)时,会默认保留年月日
这里可以发现oracle中对M和m是区分不了的,才报了格式代码出现了两次,解决方案是将mm表示分钟的改成mi表示分钟
你会发现mm变成mi后还是报错了,因为to_date(时间类型的数据)再给一个时间转化格式,to_date函数不支持了,那如果我想指保留年月该咋办?可以使用trunc函数,trunc函数专门解决这个的,我文章最后会补充这个函数
当上边的to_date 改为to_char后,就可以将时间类型的数据按照‘yyyy-MM-dd hh:mm:ss’的时间格式样式变成字符串类型了
当然,我们使用to_char可以将时间类型的保留年月日信息变成字符串,也可以保留年月信息变成字符串

那也可以to_date(符合时间格式的字符串,时间格式)将时间格式的字符串变成时间类型的数据
尝试用to_char(时间格式的字符串,对应时间格式样式)将字符串弄成字符串,直接报内容识别不了
尝试to_date(时间格式的字符串)不写对应时间格式样式,直接解析失败
to_char(时间类型数据)会按oracle中时间默认解析的时间格式样式来解析,比如我这的,就是这个样子
根据上述测试大概做个总结:
1.to_date():括号中可以是时间类型的数据,但不能再写时间格式样式,不然报错解析不了
2.to_date()括号中可以是字符串,但该字符串必须是满足时间格式的,并且要写上对应的时间格式样式,不然解析字符串失败
3.to_char()括号中可以是时间类型的数据,可以写时间格式样式,也可以不写,不写时间格式样式就会使用oracle中默认的时间格式样式来转化。
4.oracle中对大写字母和小写字母区分不了,在写时间格式样式涉及月和分的要注意,月M,分m,oracle是识别不了的,要区分一哈,oracle中规定分用mi表示,也就是月分 MM mi
2.5to_number()
to_number()函数是oracle中常用的类型转换函数之一,是将一些处理过的按一定格式编排过的字符串变回数值型的格式。我在展示long类型的id,需要使用这个,不然太长会按照科学计数法展示
1、to_number()函数可以将char或varchar2类型的string转换为一个number类型的数值;
2、需要注意的是,被转换的字符串必须符合数值类型格式,如果被转换的字符串不符合数值型格式,Oracle将抛出错误提示;
3、to_number和to_char恰好是两个相反的函数;
用法:
(1)to_number(varchar2 or char,'格式')
select to_number('000012134') from dual;
select to_number('88877') from dual;
(2)如果数字在格式范围内的话,就是正确的,否则就是错误的;如:
select to_number('$12345.678', '$999999.99') from dual;
select to_number('$12345.678', '$999999.999') from dual;
(3)可以用来实现进制转换;16进制转换为10进制:
select to_number('19f','xxx') from dual;
select to_number('f','xx') from dual;
2.6add_months
add_months 函数主要是对日期函数进行操作,在数据查询的过程中进行日期的按月增加,其形式为:
add_months(date,int);其中第一个参数为日期,第二个为按月增加的幅度,例如:
add_months (sysdate,2):就是当前日期的两个月之后的时间。注意:没有add_year,想要年的效果,在月的add_months(sysdate,12)12月就是一年,这样效果和year一样了
案例:
查询当前时间1个月以后的时间:
select add_months(sysdate,1) from dual;
查询当前时间1个月以前的时间:
select add_months(sysdate,-1) from dual;
2.7last_day
last_day()返回包含了日期参数的月份的最后一天的日期
案例:
select last_day(date'2000-02-01') "Leap Yr?" from dual
结果:29
2.8 round
round函数是用于对数字进行截取操作,且会对截取的数字进行四舍五入运算
案例:
round((sh.S_CURRENT_PRODUCT_TOTAL/sh.S_EXPECT_PRODUCT_TOTAL*100),2)
效果:数字四舍五入,保留两位小数
2.9concat
concat:拼接函数,使用场景:将多个字段合在一起,比如year字段和month字段合在一起成年月格式了
案例:
to_date(concat(sh.YEAR,sh.MONTH),'yyyyMM')=#{parm.timeInfo}
2.10 row_number() over(partition by b.pid order by p_time desc)
案例:
//实现需求是:对数据先按最新时间排序,然后我只要最新时间的pid的数据(就是数据中有一样的pid,我们要去重,但要保留最新的这个pid的数据)
select id,pid,p_time from(
select b.id,b.pid,b.p_time,row_number() over(partition by pid order by p_time) rn from SP_PRODUCER_WELL a,SP_WELL_DAILY_STATS b where a.id=b.pid and a.DEL_FLAG=0 and b.DEL_FLAG=0
order by b.P_TIME desc) bb
where rn=1
3.结语:
目前我遇到的常用的函数就这些,后续还有,我会补充,最后希望这些函数能给你的业务思路带来灵感,更易实现需求。
补充:
substr函数格式 (俗称:字符截取函数):
这篇文档写的可以,可以参考:https://www.cnblogs.com/dshore123/p/7805050.html
trunc函数:
trunc函数:用法有两种:TRUNC(NUMBER)表示截断数字,TRUNC(date)表示截断日期
截断数字:TRUNC(n1,n2),n1表示被截断的数字,n2表示要截断到那一位。n2可以是负数,表示截断小数点前。注意,TRUNC截断不是四舍五入。
截断日期:参数可以是dd,d,y,yy,mi,mm
基础数据:
案例:








参考资料:
https://www.cnblogs.com/weihuang6620/p/6903961.html
https://www.jb51.net/article/168512.htm
边栏推荐
- Deeply understand the mathematics behind deep neural networks (mysteries of neural networks Part I)
- JS prototype. slice. call(arguments); Convert pseudo array to array
- 超酷汇编教程-- 简明x86汇编语言教程(1)
- How to solve SQL import
- Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
- 如何在 2022 年为 Web 应用程序选择技术堆栈
- [open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
- go---- mgo
- Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
- Logistic Regression VS Linear Regression
猜你喜欢

VGA display of de2-115 FPGA development board

Shell built-in commands

Win7显示屏幕亮度在哪里可以调节

Logistic Regression VS Linear Regression

Gorm---- Association query

Test your earning power? What will you do in the future?

"Mobile cloud Cup" computing power network application innovation competition is in hot registration!

Nine parts of speech and nine tenses in English

Numpy库使用入门

What if the CPU temperature of Dell computer is too high
随机推荐
Beego--- notes
K-line diagram 24 classic diagrams (shadow)
Qt内存映射
多台云服务器的 Kubernetes 集群搭建
【开源鸿蒙系统展示】RK3568开发板搭载OpenHarmony 3.1 Release
JS prototype. slice. call(arguments); Convert pseudo array to array
Solving error: creating window glfw error: glew initialization error: missing GL version
k线图24种经典图解(影线篇)
Is it safe for tongdaxin to open a stock account
戴尔电脑cpu温度过高怎么办
How to choose a technology stack for web applications in 2022
Rust, le meilleur choix pour un programmeur de démarrer une entreprise?
Use of bind() in JS and handwritten bind()
删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?
分类器与cross entropy loss函数
[proteus simulation] 51 MCU +ds1302+lcd1602 display
shell 数组
一次性总结:64个数据分析常用术语!
sql导入这样怎么解决
BACnet gateway bl103 for building automation