当前位置:网站首页>Oracle uses data manipulation functions
Oracle uses data manipulation functions
2022-07-21 23:52:00 【zhiyou-rookie】
Oracle Use data manipulation functions
One 、 summary :
Like almost all other computer languages ,SQL Support the use of functions to manipulate data , Functions are usually used to perform operations on data , For conversion and operation .
Two 、 The function classification
- Text functions : Used to manipulate text strings ( for example , Trim or fill values , And converting values to upper and lower case ).
- Numerical function : Used to perform mathematical operations on numerical data ( for example : Return absolute values and perform algebraic calculations ).
- Date and time functions : Used to operate date and time values , And extract specific parts from these values ( for example : Return the difference between dates and check the validity of dates ).
- System function : Returns all specific DBMS Information about ( for example : Return the user login information or check the version details ).
1. Text manipulation function
Example :
SELECT vend_name, Upper(vend_name) AS vend_name_upcase
FROM vendors
ORDER BY vend_name;
result :
It can be seen that ,Upper() Convert text to uppercase . The following table lists some commonly used text manipulation functions .
function | describe |
---|---|
Length() | Returns the length of the string |
Lower() | Convert a string to lowercase |
LPad() | Fill the left side of the string with spaces |
LTrim() | Trim white space from the left of the string |
RPad() | Fill in spaces on the right side of the string |
RTrim() | Trim white space from the right side of the string |
Soundex() | Return string SOUNDEX value |
SubString() | Returns the characters in the string |
Upper | Convert a string to uppercase |
--1、 Query character length bits 6 Number of numbers :length()
select * from table_name where length(column_name) = 6;
--2、 Convert a string to lowercase :lower()
select lower(column_name) from table_name;
--3、 Fill the left side of the string :lpad(String , Interception length , Added string ). Adding a string is not accurate , A more accurate statement is that String Intercept the string , If the intercept length is greater than String The length of , It's in String Add a string to the left of the to fill , If the third parameter is not specified , Then fill in the blanks .
select lpad('test',10,'ee') from dual;
-- result :
lpad('test',10,'ee')
----------------------------
eeeeeetest
————————————————------------
--4、 Trim white space from the left of the string : LTRIM(c1,[,c2])
【 function 】 Delete the string that appears on the left
【 Parameters 】C1 character string ,c2 Append string , Default is space
【 return 】 Character
select ltrim('abcddee','abc') from dual;
-- result :
ltrim('abcddee','abc')
---------------------------
ddee
————————————————-----------
--5、 Fill the right side of the string :rpad(string, c1, [c2])
【 function 】 Fill the right side of the string
【 Parameters 】string: String to be operated on c1: String length after operation c2: Operator for padding , Default is space
--6、 Trim white space from the right side of the string : RTRIM(c1,[,c2])
【 function 】 Delete the string that appears on the right
【 Parameters 】c1: character string c2: String to be deleted
【 return 】 Character
select rtrim('abcddee','ddee') from dual;
-- result :
ltrim('abcddee','ddee')
---------------------------
abc
————————————————-----------
--7、 Returns the sub characters in the string :substring(string, index1, [,length1])
【 function 】 Returns the substring in the string
【 Parameters 】string: The source string of the operation index1: The substring is at the beginning of the source string length1: Length of substring
【 return 】 Character
select substring('string', 0, 3) from dual;
-- result :
substr('string', 0, 3)
---------------------------
str
————————————————-----------
--8、 Returns the uppercase form of a string :upper(string)
【 function 】 Returns the uppercase form of a string
【 Parameters 】:string: The source string that needs operation
【 return 】 Character
select upper('string') from dual;
-- result :
upper('string')
---------------------------
STRING
————————————————-----------
Be careful :
SOUNDEX It's an algorithm , Used to convert any text string into an alphanumeric pattern , Describe the voice representation of the text .SOUNDEX Similar pronunciation characters and syllables are considered , Allow pronunciation by string ( Instead of how they are entered ) To compare them . Even though SOUNDEX Is not a SQL Concept ,Oracle( Like many others DBMS equally ) It still provides the right SOUNDEX Support for .
Here is an example of using Soundex() Example of a function .customers There is one in the table Coyote Inc. Customers , Contact name is Y. Lee. however , If there is an input error , The contact person is actually Y. Lie, What will happen ? obviously , Searching by the correct contact name will not return any data , As shown below :
SELECT cust_name, cust_contact
FROM customers
where cust_contact = 'Y. Lie'
Now try using Soundex() Function performs the same search , To match pronunciation similar to Y. Lie Names of all contacts .
SELECT cust_name, cust_contact
FROM customers
WHERE Soundex(cust_contact) = Soundex('Y Lie');
In this example ,WHERE Clause use Soundex() Function cust_contact Column values and search strings are converted to their SOUNDEX value . because Y. Lee and Y. Lie Sound alike , Their SOUNDEX The value will match , therefore WHERE Clause will correctly filter out the desired data .
2. Date and time manipulation functions
The following table lists some commonly used date and time operation functions
function | describe |
---|---|
Add_Mounth() | Add a month to the date ( You can also subtract the month ) |
Extract() | Subtract last year from the date and time , month , Japan , when , Minutes or seconds |
Last_Day() | Return to the last day of the month |
Months_Between() | Returns the number of months between two months |
Next_Day() | Return to the day after the specified date |
Sysdate() | Returns the current date and time |
To_Date() | Convert a string to a date |
-- Use cases ;
--1、 Query the current system time plus 3 Time after month :add_month()
select add_month(sysdate,3) from dual;
--2、 Inquire about 6 Monthly data :extract()
select * from extract(month from date1);
--3、 Query the last day of the month :last_day() Hypothetical bit 2022/6
select last_day(sysdate) from dual;
--> result :2022/06/30
--4、 Query the number of months between two times :months_between()
select months_between(to_date('2014-3-21','yyyy-mm-dd'), to_date('2014-1-10','yyyy-mm-dd')) months from dual;
MONTHS
----------
2.3548387
————————————————
--5、 Returns the day after the specified date :next_date()
select sysdate from dual;
SYSDATE
-------------------
2022-07-13 19:30:26
--6、 Returns the current date and time :sysdate()
select sysdate from dual;
--7、 Convert a string to a date :to_date()
select * from table_name where date1 = to_date('2022/07/13', 'yyyy/mm/dd');
Be careful : The most important Extract() function
One thing to remember : Data formatting can be tricky . After all ,”2022-03-04“ It refers to that day ?03 Is the month and 04 Is it heaven ? Or vice versa ? therefore , Whenever a date is provided to Oracle, Must clearly explain how it is formatted .
SELECT cust_id, order_num
FROM orders
WHERE order_date = TO_DATE('2015-02-01', 'yyyy-mm-dd');
More flexible date operations require the ability to extract specific parts of the date or time . here ,Extract() Functions come in handy . seeing the name of a thing one thinks of its function ,Extract() Used to extract some parts of the date and time , It is allowed to process only YEAR、MONTH、DAY、HOUR、MINUTE and SECOND.
Here is another solution to the previous problem ( This solution does not require you to figure out how many days there are in a month , Or worry about the leap year 2 month ):
SELECT cust_id, order_num
FROM orders
WHERE Extract(Year FROM order_date) = 2015
AND Extract(Month FROM order_date) = 2
analysis :
Extract(Year) Returns the year in the date . Similarly ,Extract(Month) Return the month in the date . therefore ,WHERE Extract(Year FROM order_date) = 2015 AND Extract(Month FROM order_date) = 2 Will retrieve order_date stay 2015 Years and 2 All lines of the month .
3. Numeric operation function
Numerical manipulation functions are of course used to manipulate numerical data . These functions only need to be used in algebra , Trigonometric or geometric operations , Therefore, it is not as commonly used as string or date and time manipulation functions .
The following table lists the commonly used numerical operation functions :
function | describe |
---|---|
Abs() | Returns the absolute value of the number |
Cos() | Returns the trigonometric cosine of the specified angle |
exp() | Returns the exponent value of a specified number |
mod() | Returns the remainder of the division operation |
sin() | Returns the sine of a specified angle |
sqrt() | Returns the square root of a specified number |
tan() | Returns the trigonometric tangent of the specified angle |
边栏推荐
- Upload and download files of JMeter
- Basic MD5 encryption
- JMeter之上传文件和下载文件
- Day01 software testing foundation summary
- Install cross compiler: eabi-4.3.3_ EmbedSky_ 20100610.tar. bz2
- 页面重定向
- Follow the guidelines and improve yourself quickly: how to quickly turn functional testing to automated testing
- Is it feasible to enter software testing at the age of 28?
- Common assertions for judging the success of running results when using postman batch running interface
- MySQL learning notes
猜你喜欢
棋盘覆盖问题
JMeter之响应断言
Use Mysql to query logs slowly
Does Navicat 16 support native Apple silicon M1 chips| Apple user must read
"Polite interaction, thank you for your participation" and you will have the opportunity to receive Navicat premium 16
Lamp架构——mysql高可用切换(MHA高可用)
LoadRunner clears browser cache
File download, how to write the use case?
Jenkins build
How to do app upgrade test?
随机推荐
Postman interface test and pressure test
Store metadata in MySQL and PostgreSQL
What if win7 system forgets its login password? (without startup disk)
WebService (soap) request of JMeter
MySQL learning notes
Lamp架构——mysq集群及主从复制(2)
动态规划求解(添+号求最小值和问题)
Pychart 2019 usage settings make it easier for you to use!
Test point exercise
Page redirection
Peoplecode variable
Audio and video learning notes (Thor) - Technical Analysis
Lamp Architecture - MySQL router (read / write separator)
Analysis of cross apply and outer apply query of SQL Server - Part II
软件测试常问面试题【二】
How to do app installation test?
LAMP架構——mysql路由器(讀寫分離器)
安装交叉编译器:EABI-4.3.3_EmbedSky_20100610.tar.bz2
【初识Jmeter和线程组】
JMeter之WebService(soap)请求