本文摘自并翻译于《Oracle Database 10g 完全参考手册》英文版
LTRIM, RTRIM, and TRIM
LTRIM andRTRIMare like hedge trimmers.They trim off unwanted characters from the left and
right ends of strings.(翻译:他们从串的左边或右边删除不需要的字符。) For example,suppose you have a MAGAZINE table with a column in it that
contains the titles of magazine articles, but the titles were entered bydifferent people. Some people
always put the titles in quotes, whereas others simply entered the words;some used periods, others didn’t; some started titles with “The,” whereasothers did not. How do you trim these?
select Title from MAGAZINE;
TITLE
-------------------------------------
THE BARBERS WHO SHAVE THEMSELVES.
"HUNTING THOREAU IN NEW HAMPSHIRE"
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
"INTERCONTINENTAL RELATIONS."
Here are the formats for RTRIMandLTRIM:
RTRIM(string [,'set'])
LTRIM(string [,'set'])
Here, string is the name of the column from the database (or a literalstring), and set is the collection
of characters you want to trim off. If no set of characters is specified,the functions trim off spaces.
You can trim off more than one character at a time; to do so, simply makea list (a string) of the
characters you want removed. First, let’s get rid of the quotes andperiods on the right, as shown here:
select RTRIM(Title,'."') from MAGAZINE
Set of characters being trimmed
The preceding produces this:
RTRIM(TITLE,'."')
-----------------------------------
THE BARBERS WHO SHAVE THEMSELVES
"HUNTING THOREAU IN NEW HAMPSHIRE
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
"INTERCONTINENTAL RELATIONS
RTRIM removed both the double quotationmarks and the periods from the right side of each
of these titles. The set of characters youwant to remove can be as long as you want. Oracle will
check and recheck the right side ofeach title until every character in your string has been removed—that is, untilit runs into the first character in the string that is not in your set.(翻译:要删除的字符集可以根据需要设为任意长度。Oracle将反复检查每个标题的右端,直到串中的每个字符串都被删除了,也就是说,知道它遇到被删除字符集中未出现的第一个字符时,才停止删除。)
Combining Two Functions
Now what? You can use the LTRIMfunction to get rid of the quotes onthe left. The Title column
is buried in the middle of the RTRIM function. In this section, you learnhow to combine functions.
You know that when you ran the select statement
select Title from MAGAZINE;
the result you got back was the content of the Title column, as shownnext:
THE BARBERS WHO SHAVE THEMSELVES.
"HUNTING THOREAU IN NEWHAMPSHIRE"
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
"INTERCONTINENTALRELATIONS."
Remember that the purpose of
RTRIM(Title,'."')
is to take each of these strings and remove the quotes on the right side,effectively producing a
result that is a new column whose contents are shown here:
THE BARBERS WHO SHAVE THEMSELVES
"HUNTING THOREAU IN NEWHAMPSHIRE
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
"INTERCONTINENTAL RELATIONS
Therefore, if you pretend that RTRIM(Title,'."') is simply a column name itself, you can substitute
it for string in the following(翻译:因此,如果假定RTRIM(Title,'."')只是一个列名,那么可以用它来代替下面的string):
LTRIM(string,'set')
So you simply type your selectstatement to look like this:
selectLTRIM(RTRIM(Title,'."'),'"') from MAGAZINE;
Taking this apart for clarity, you see the following:
Is this how you want it? And what is the result of this combined function?
LTRIM(RTRIM(TITLE,'."'),'"')
-----------------------------------
THE BARBERS WHO SHAVE THEMSELVES
HUNTING THOREAU IN NEW HAMPSHIRE
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
INTERCONTINENTAL RELATIONS
Your titles are now cleaned up.
Looking at a combination of functions the first (or the thousandth) timecan be confusing,
even for an experienced query user. It’s difficult to assess which commasand parentheses go with
which functions, particularly when a query you’ve written isn’t workingcorrectly; discovering
where a comma is missing, or which parenthesis isn’t properly matched withanother, can be a
real adventure.
One simple solution to this is to break functions onto separate lines, atleast until they’re all
working the way you want. SQLPLUS doesn’tcare at all where you break a SQL statement, as
long as it’s not in the middle of aword or a literal string. To better visualize how thisRTRIMand
LTRIM combination works, you could type it like this:
(翻译:SQLPLUS根本不关心在哪里拆分了SQL语句,只要不是在一个单词或一个字面量字符串的中间拆开即可。为了更好的理解RTRIM和LTRIM的组合是如何工作的,可按如下方式输入函数)
select LTRIM(
RTRIM(Title,'."')
,'"')
from MAGAZINE;
This makes what you are trying to do obvious, and it will work even if itis typed on four separate lines with lots of spaces. SQLPLUS simply ignoresextra spaces.
***********************************************************************************************************
(提示:下面要说的是重点,可能被一些人忽略的)
Suppose now you decide to trim offTHE from the front of two of the titles, as well as the space
that follows it (and, of course, thedouble quote you removed before). You might do this:
(翻译:现在,假定要删除两个标题前的THE以及跟在其后的空格(当然,还有之前要删去的双引号)。可以使用下面的代码)
select LTRIM(RTRIM(Title,'."'),'"THE ')
from MAGAZINE;
which produces the following:
LTRIM(RTRIM(TITLE,'."'),'"THE')
-----------------------------------
BARBERSWHO SHAVE THEMSELVES
UNTING THOREAU IN NEW HAMPSHIRE
NIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
INTERCONTINENTAL RELATIONS
What happened? The second and third row got trimmed more than expected.Why? Because
LTRIM was busy looking for and trimmingoff anything that was a double quote, a T, an H, an E,
or a space. It was not looking for the word THE. It was looking for theletters in it, andLTRIM
didn’t quit the first time it saw any of the letters it was looking for.It quit when it saw a character
that wasn’t in its set.
(翻译:发生什么事情了?删除了第2行和第3行不期望删除的内容。为什么呢?因为LTRIM忙于查找并删除双引号、T、H、E或空格。它并不是在查找单词THE,而是在查找其中的字母。当LTRIM第一次发现它所要查找的字母时,它并不退出。而当它发现不属于其字符集中的字符时,才会退出。)
In other words, all of the following and many other combinations of theletters will have the
same effect when used as the set of anLTRIMorRTRIM:
(翻译:换句话说,以下给出的字母组合以及许多其他的字母组合在用于LTRIM或RTRIM时都会产生同样的效果:)
'"THE'
'HET"'
'E"TH'
'H"TE'
'ET"H'
The order of the letters ofthe set has no effect on how the function works. Note, however, that the caseof the letters is important. Oracle will check the case of both the letters inthe set and in the string. It will remove only those with an exact match.
LTRIM andRTRIMare designed to remove any characters in a set from the left or right of a
string. They’re not intended toremove words. To do that requires clever use ofINSTR,SUBSTR,
and even DECODE, which you will learn about inChapter 16.
(翻译:LTRIMandRTRIM用来从串的左边或者右边删除字符集中的任何字符。他们并不删除单词。为了做到这一点,需要灵活运用INSTR,SUBSTR, andDECODE 函数,这些函数将在第16章中介绍。)
The previous example makes one point clear: It’s better to make certainthat data gets cleaned
up or edited before it is stored in the database. It would have been a lotless trouble if the individuals
typing these magazine article titles had simply avoided the use of quotes,periods, and the word THE.
Using the TRIM Function
The preceding example showed how to combine two functions—a useful skillwhen dealing with
string manipulation. If you are trimming the exact same data from both thebeginning and the end
of the string,you can use the TRIMfunction in place of anLTRIM/RTRIMcombination.
TRIM uses a unique syntax. The followingexample shows the use of theTRIMfunction with its
associated fromclause within the function. In this example, the doublequotes are removed from
the beginning and the end of the magazine article titles.Becausethe double quote is a character
string, it is placed inside two single quotes:
select TRIM('"' from Title) from MAGAZINE;
TRIM('"'FROMTITLE)
-----------------------------------
THE BARBERS WHO SHAVE THEMSELVES.
HUNTING THOREAU IN NEW HAMPSHIRE
THE ETHNIC NEIGHBORHOOD
RELATIONAL DESIGN AND ENTHALPY
INTERCONTINENTAL RELATIONS.
The quotes have been removed from the beginning and ending of the strings.If you just want to
trim one end of the strings, you could use theleadingortrailingclause, as shown in the following
listing:
select TRIM(leading '"' from Title) from MAGAZINE;
select TRIM(trailing '"' from Title) from MAGAZINE;
Using leadingmakesTRIMact likeLTRIM;trailingmakes it act likeRTRIM. The most powerful
use of TRIMis its ability to act on both endsof the string at once, thus simplifying the code you
have towrite—provided the same characters are being removed from both ends of thestring.
Adding One More Function
Suppose that you decide to RPADyour trimmed-up titles with dashesand carets, perhaps also
asking for a magazine name and page number. Your query would look likethis:
selectName, RPAD(LTRIM(RTRIM(Title,'"'),'."'),47,'-^'), Page
fromMAGAZINE;
NAME RPAD(LTRIM(RTRIM(TITLE,'"'),'."'),47,'-^') PAGE
--------- ------------------------------------------------- -----
BERTRAND MONTHLY THE BARBERS WHO SHAVE THEMSELVES-^-^-^-^-^ 70
LIVE FREE OR DIE HUNTING THOREAU IN NEW HAMPSHIRE-^-^-^-^-^ 320
PSYCHOLOGICA THE ETHNIC NEIGHBORHOOD-^-^-^-^-^-^-^-^-^- 246
FADED ISSUES RELATIONAL DESIGN AND ENTHALPY-^-^-^-^-^-^ 279
ENTROPY WIT INTERCONTINENTAL RELATIONS-^-^-^-^-^-^-^-^ 20
Each function has parentheses that enclose the column it is going toaffect, so the real trick in
understanding combined functions in select statements is to read from theoutside to the inside
on both theleft and right, watching (and even counting) the pairs of parentheses.