当前位置:网站首页>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
边栏推荐
- 网络远程访问的方式使用树莓派
- 电脑必须打开的设置
- 深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
- Kubernetes cluster construction of multiple ECS
- API encapsulation of uniapp applet
- Nr-arfcn and channel grid, synchronous grid and GSCN
- 英語中的九大詞性與九大時態
- 请问通达信股票开户是安全的吗?
- 多臺雲服務器的 Kubernetes 集群搭建
- Discuz copy today's headlines template /discuz news and information business GBK template
猜你喜欢

Data acquisition system gateway acquisition plant efficiency

Preventing overfitting of deep neural networks (mysteries of neural networks Part II)

Graph contractual learning with augmentations

Realization of neural networks with numpy

Where can the brightness of win7 display screen be adjusted

Getting started with shell variables

China has made major breakthroughs in battery technology. Japan, South Korea and the United States are lagging behind. China has consolidated its leading edge

论文阅读:Graph Contrastive Learning with Augmentations

分类器与cross entropy loss函数
![[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release](/img/c4/0bfb380d38b5205a02a10175ee6888.png)
[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
随机推荐
How to choose a technology stack for web applications in 2022
Websocket -- reverse proxy to solve cross domain problems
Is it safe for Guosen Securities to open an account?
None of the MLIR optimization passes are enabled (registered 2) solutions
让PyTorch训练速度更快,你需要掌握这17种方法
论文阅读:Graph Contrastive Learning with Augmentations
【开源鸿蒙系统展示】RK3568开发板搭载OpenHarmony 3.1 Release
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
go---- mgo
NVM installation and use tutorial
Renix perf: detailed explanation of IP network performance test tools and test case parameters
请问通达信股票开户是安全的吗?
Is it safe for Guosen Securities to open a stock account? Excuse me?
shell 运算符
[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release
Shell array
Syntax 'trap'
Qt内存映射
Graph contractual learning with augmentations
触觉智能分享-RK3568在金融自助终端的应用