当前位置:网站首页>Oracle one line function Encyclopedia

Oracle one line function Encyclopedia

2022-06-25 08:53:00 Mr. Li, a prodigy

If a worker wants to do a good job, he must sharpen his tools first
Articles are constantly updated , You can search by wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 Access to benefits , reply 【 project 】 Get the source code of the project , reply 【 The resume template 】 Get resume template , reply 【 Learning Roadmap 】 Get a learning roadmap .

 Insert picture description here

List of articles


One 、 String function

Convert to uppercase function :upper
Convert to lowercase function :lower
The first letter becomes uppercase :initcap
Calculate string length :length
String substitution :replace( class , The value to replace , Replace with value )
String interception :substr( Column , The starting point , length )

1、 Look up the student's name and capitalize

select upper(name) from student;

2、 Look up the student's name in lowercase

select lower(name) from student;

3、 take "helloword" title case

Be careful dual yes oracle A self-contained watch

select initcap('helloword') from dual;

result :Helloword

4、 Check all student names and capitalize

Either of the following is OK

select initcap(name) from student;

5、 Check the names of all students , And display the length of the name

select name, length(name) from student;

6、 Check the names of all students , And will “ Zhang San ” Change it to “ Li Si ”

select replace(name,' Zhang San ',' Li Si ') from student;

7、 take "hello word" Remove the space in

select replace('hello word',' ','') from dual;

9、 Find out the last names of all students

Be careful , The initial character interception is from 1 At the beginning , If the initial value is written as 0 It will also default from 1 Start

select substr(name,1,1) from student;

Two 、 Numerical function

rounding :round( data , Keep decimal places )
Directly intercept decimal places , No rounding :trunc( data , Keep decimal places )
Modulus function :mod( Divisor , Divisor )

1、 take 123.4567 Keep two decimal places

select round(123.4567,2) from dual;

result :123.46

2、 take 123.4567 Keep the first two decimal places , Less than 5 Give up

select round(123.4567,-2) from dual;

result :100

3、 take -123.65 Integer

select round(-123.65) from dual;

result :-124

4、 take 123.4567 Cut directly to two decimal places

select trunc(123.4567) from dual;

result :123.45

5、 take -123.65 Directly truncate to an integer

select trunc(-123.65) from dual;

result :-123

6、 take 10 And 3 modulus

select mod(10,3) from dual;

result :1

3、 ... and 、 Date function

oracle The self-contained current date data column :sysdate
Calculate the month of difference between two dates :months_between( date 1, date 2)
Add a date after several months :add_months( date , Monthly number )
Query the last day of the month :last_day( date )
Calculate the next specified date :next_day( date , Time in a week )

1、 Query the current date

Be careful : The query here is oracle The date the database was installed on the computer , such as oracle Install on server , In this way, there will be a gap between the date when you use your local computer to remotely query the database and the date on your local computer , It's usually 1 About minutes .

select sysdate from dual;

2、 Query the date after three days

select sysdate + 3 from dual; 

3、 Query the number of months between two dates

select months_between(date1,date2) from dual;

4、 Query the year difference between two dates

select trunc(months_between(date1,date2)/12) years from dual;

5、 Query the current date 5 Months later

select add_months(sysdate,5) from dual;

6、 Query the last day of the month

select last_day(sysdate) from dual;

7、 Check next Wednesday

select next_day(sysdate,' Wednesday ') from dual;

Four 、 Conversion function

Convert to string :to_char()
Convert to date :to_date()
Convert to number :to_number()

1、 Format the current date as yyyy-mm-dd Format string

select to_char(sysdate,'yyyy-mm-dd') from dual;

2、 Query the year of the current date

select to_char(sysdate,'yyyy') year from dual;

3、 take “1998-09-27” Convert to date format

select to_date('1998-09-27','yyyy-mm-dd') from dual;

4、 take “1” Convert to numeric format

select to_number('1') from dual;

5、 ... and 、 The generic function

If it is empty, the defined value is used , If it is not empty, the original value will be used :nvl( Column , Definition value )
Multi value judgment :decode( Column , Match content 1, According to the content 1, Match content 2, According to the content 2)

1、 Look up the names of the students , If the name is empty, use “ Zhang San ” Instead of

select nvl(name,' Zhang San ') from student;

2、 If the age of the student is blank, use 0 Instead of

select nvl(age,0) from student;

3、 Check the gender of the student , And will “ male ” Corresponding “man”, take “ Woman ” Corresponding “woman”

select decode(sex,' male ','man',' Woman ','woman') from student;

6、 ... and 、 summary

The relevant contents here have not been sorted out yet , The article continues to be updated later , Recommended collection .

The commands involved in the article must be typed several times each like me , Only in the process of knocking can you find out whether you really master the command .

You can search by wechat 【 Xiaoqi JAVA interview 】 First time reading , reply 【 Information 】 Access to benefits , reply 【 project 】 Get the source code of the project , reply 【 The resume template 】 Get resume template , reply 【 Learning Roadmap 】 Get a learning roadmap .

原网站

版权声明
本文为[Mr. Li, a prodigy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250813358469.html