当前位置:网站首页>Summary of common functions in Oracle Database
Summary of common functions in Oracle Database
2022-06-25 14:15:00 【Peppermint brain flower】
oracle Database commonly used function summary
1. Preface
In daily development , Some companies use oracle The database acts as a data storage container for the project , I will summarize the usage of some functions encountered by myself .
2. Summarized functions
2.1extract
extract:
oracle in extract() Function from oracle 9i Introduction in , Used from a date perhaps interval A specific part of a type is intercepted
usage :
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 The function is based on the fact that a given number is a positive number , Just go back to 1,0 return 0, A negative number returns -1, I think this function is used a lot . We need to pay attention to sign( There can only be one field in this bracket ), I tested , Writing a subquery in parentheses directly reports an error
usage :
// demand : Check items , It is necessary to distinguish the profitability of the item , such as goods_interest Field , Yes 0.23,0,-0.23 etc.
select sign(goods_interest)as interest_flag from goods
// Effect is :1,0,-1
2.3decode
decode function : Is to take a value for a certain value , I think it is very easy to use , It feels like java Medium switch Usage of , Save in the field char Type numbers represent different meanings of Chinese characters , By this function , Find out , The front end can directly display , There is no need to change another Chinese character , The back-end export is also one-step , No need to use switch Convert Chinese characters , Simplify the code .
usage :
decode( Conditions , value 1, Return value 1, value 2, Return value 2,… value n, Return value n, The default value )
select
decode(PROBLEM_LEVEL,1,' commonly ',2,' more ',3,' major ')as PROBLEM_LEVEL
from accident
// effect :
If PROBLEM_LEVEL yes 2, It turns out to be a big one
2.4to_date and to_char
to_date and to_char: Because these two functions are used too often , Explain together ;to_date: Yes, it will oracle The time style string or time type data in is formatted as time type data (yyyy-MM-dd,yyyy-MM And so on ),to_char Is to change the current value into a string type , In terms of time processing , Time type can be converted to string matching , For example, the time type of the database , But the background receives a string , The format is yyyy-MM, Then you can convert the string ratio , You can also turn the time ratio , But note that only the month and year are compared , Other content interception
Case study :
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')
Yes to_char and to_date The complement of function :
Test code :
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)
Give an example by system time :
When to_date( Time type data ) when , The date will be reserved by default 
You can find it here oracle Chinese vs M and m Is indistinguishable , The format code appears twice , The solution is to mm The minute is changed to mi Represents minutes 
You'll find that mm become mi After that, I still reported an error , because to_date( Time type data ) Give another time to convert the format ,to_date Function does not support , What should I do if I want to refer to the retention date ? have access to trunc function ,trunc Function to solve this problem , This function will be added at the end of my article 
Be the one above to_date Change it to to_char after , Time type data can be divided into ‘yyyy-MM-dd hh:mm:ss’ The time format style of is changed to string type 
Of course , We use to_char You can change the reserved year, month and day information of time type into a string , You can also keep the year and year information into a string 

That can be to_date( String in time format , Time format ) Change the time format string into time type data 
Try to use to_char( String in time format , Corresponding time format style ) Make a string into a string , The contents of the direct report cannot be recognized 
Try to_date( String in time format ) Do not write the corresponding time format style , Direct parsing failed 
to_char( Time type data ) Will press oracle The default time format style for time resolution in , Like my , That's what it looks like 
Make a summary according to the above test :
1.to_date(): Data in parentheses can be of time type , But you can no longer write the time format style , Otherwise, the error report cannot be resolved
2.to_date() The parentheses can be strings , But the string must be in a time format , And write the corresponding time format style , Otherwise, parsing the string fails
3.to_char() Data in parentheses can be of time type , You can write time format styles , Or not , Without writing the time format style, you will use oracle The default time format style in .
4.oracle There is no distinction between uppercase and lowercase letters , When writing time format styles involving months and minutes, you should pay attention to , month M, branch m,oracle It's not recognizable , It is necessary to distinguish ,oracle It is specified in mi Express , That is, the month minute MM mi
2.5to_number()
to_number() The function is oracle One of the commonly used type conversion functions in , It is to change some processed strings arranged in a certain format back to the numerical format . I'm showing long Type of id, You need to use this , Otherwise, too long will be displayed according to the scientific counting method
1、to_number() The function can change char or varchar2 Type of string Convert to a number Type value ;
2、 It should be noted that , The converted string must conform to the format of numeric type , If the converted string does not conform to the numeric format ,Oracle An error message will be thrown ;
3、to_number and to_char Just two opposite functions ;
usage :
(1)to_number(varchar2 or char,' Format ')
select to_number('000012134') from dual;
select to_number('88877') from dual;
(2) If the number is within the format range , That's right , Otherwise it would be wrong ; Such as :
select to_number('$12345.678', '$999999.99') from dual;
select to_number('$12345.678', '$999999.999') from dual;
(3) Can be used to achieve binary conversion ;16 Base to zero 10 Base number :
select to_number('19f','xxx') from dual;
select to_number('f','xx') from dual;
2.6add_months
add_months Function is mainly used to operate the date function , In the process of data query, the date is increased by month , In the form of :
add_months(date,int); The first parameter is date , The second is the increase by month , for example :
add_months (sysdate,2): It's two months after the current date . Be careful : No, add_year, Want the effect of years , In the month of add_months(sysdate,12)12 Month is a year , This effect and year The same
Case study :
Query the current time 1 Months later :
select add_months(sysdate,1) from dual;
Query the current time 1 Months ago :
select add_months(sysdate,-1) from dual;
2.7last_day
last_day() Returns the date of the last day of the month with the date parameter
Case study :
select last_day(date'2000-02-01') "Leap Yr?" from dual
result :29
2.8 round
round Function is used to intercept numbers , And will perform rounding operation on the intercepted numbers
Case study :
round((sh.S_CURRENT_PRODUCT_TOTAL/sh.S_EXPECT_PRODUCT_TOTAL*100),2)
effect : The number is rounded off , Keep two decimal places
2.9concat
concat: Splicing function , Use scenarios : Combine multiple fields , such as year Fields and month The fields are put together in the adult month format
Case study :
to_date(concat(sh.YEAR,sh.MONTH),'yyyyMM')=#{parm.timeInfo}
2.10 row_number() over(partition by b.pid order by p_time desc)
Case study :
// The realization requirements are : Sort the data by the latest time first , Then I just want the latest time pid The data of ( It is the same in the data pid, We're going to rehab , But keep the latest one pid The data of )
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. Conclusion :
These are the common functions I have encountered so far , And then there's , I will add , Finally, I hope these functions can bring inspiration to your business ideas , Easier to implement requirements .
Add :
substr Function format ( Be commonly called : Character truncation function ):
This document can , You can refer to :https://www.cnblogs.com/dshore123/p/7805050.html
trunc function :
trunc function : There are two ways to use it :TRUNC(NUMBER) Represents a truncated number ,TRUNC(date) Indicates the cut-off date
Truncate numbers :TRUNC(n1,n2),n1 Represents a truncated number ,n2 It means to cut off to that one .n2 It could be negative , Before truncating the decimal point . Be careful ,TRUNC Truncation is not rounding .
Cut off date : Parameters can be dd,d,y,yy,mi,mm
Basic data :
Case study :








Reference material :
https://www.cnblogs.com/weihuang6620/p/6903961.html
https://www.jb51.net/article/168512.htm
边栏推荐
- 【世界历史】第二集——文明的曙光
- Network remote access using raspberry pie
- Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
- Shell string variable
- 阻止深度神经网络过拟合(Mysteries of Neural Networks Part II)
- Table de hachage, conflit de hachage
- Realization of neural networks with numpy
- As a software testing engineer, how do you think to ensure software quality?
- Asp. Net webform exporting excel using npoi
- Hash table, hash conflict
猜你喜欢

shell 数组

112页机器学习-数学基础回顾.pptx

How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?

多臺雲服務器的 Kubernetes 集群搭建

Stream竟然还有应用进阶学习?作为程序员的你知道吗

Discuz copy today's headlines template /discuz news and information business GBK template

Shell array

Sigmoid function sigmoid derivation

Page 112 machine learning - review of fundamentals of mathematics pptx

Getting started with numpy Library
随机推荐
shell 数组
Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
shell 字符串变量
Is it safe for Guosen Securities to open an account?
Renix Perf: IP网络性能测试工具及测试用例参数详解
哈希錶、哈希沖突
JVM 用工具分析OOM经典案例
Nine parts of speech and nine tenses in English
历史上的今天:网易成立;首届消费电子展召开;世界上第一次网络直播
Complete and detailed compilation of experimental reports
两种方法实现pycharm中代码回滚到指定版本(附带截图展示)
turtlebot+lms111+gmapping实践
分享自己平时使用的socket多客户端通信的代码技术点和软件使用
删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?
Syntax 'trap'
shell 变量 入门
Les neuf caractéristiques de la parole et les neuf temps en anglais
Typescript and go --- essence
None of the MLIR optimization passes are enabled (registered 2) solutions
Graph contractual learning with augmentations