trim/ltrim/rtrim

LTRIM, RTRIM and TRIM usage in ORACLE:
. 1, LTRIM (C1, C2)
where C1 and C2 are strings, for example, C1 is' Miss Liu ', C2'MisL' and the like. This is the first and SQL SERVER not the same place. If you remember correctly LTRIM SQL Server is only one parameter, a string role is to remove the left side of the box. LTRIM and Oracle's first character is guaranteed not appear in the C1 C2 string.

SQL> select LTRIM( 'Miss Liu', 'Liu') Result  from dual;

RESULT
--------
Miss Liu

 

SQL> select LTRIM( 'Miss Liu', 'M is') result from dual;

RES
---
Liu

 

From the above it can be seen that the effect LTRIM. But if the second string is not input, then the role of LTRIM and SQL SERVER in respect of the same, is to remove the left spaces.

 

SQL> select ltrim( '  Miss Liu  ' ) result from dual;

RESULT
----------
Miss Liu

SQL> select length( '  Miss Liu  ' ) len1, length( ltrim( '  Miss Liu  ' ) ) lentrim from dual;

      LEN1 LENTRIM
    ---------- ----------
        12 10

As can be seen from the above LTrim Oracle's functions should be more powerful, it can operate the preamble.

2, and the function LTRIM RTRIM same, but modified into RTRIM right to left, so that the child is removed with a preamble in a particular character.

3, TRIM functions described as follows:


In Oracle/PLSQL, the trim function removes all specified characters either from the beginning or the ending of a string.

The syntax for the trim function is:

trim( [ leading | trailing | both  [ trim_character ]  ]   string1 )

leading - remove trim_string from the front of string1.

trailing - remove trim_string from the end of string1.

both - remove trim_string from the front and end of string1.

If none of these are chosen (ie: leading, trailing, both), the trim function will remove trim_string from both the front and end of string1.

 

trim_character is the character that will be removed from string1. If this parameter is omitted, the trim function will remove all leading and trailing spaces from string1.

string1 is the string to trim.

trim('   tech   ') would return 'tech'
trim(' '  from  '   tech   ') would return 'tech'
trim(leading '0' from '000123') would return '123'
trim(trailing '1' from 'Tech1') would return 'Tech'
trim(both '1' from '123Tech111') would return '23Tech

 

If the mentioned TRIM function, the simplest function is to use it to remove a string of spaces beginning of the line and end of the line, this feature is that you use one of the highest frequency.
However TRIM function is actually having to delete "any given" character function, which is quite cattle. Let's first experience trip.

1. look at the complete syntax description TRIM function
TRIM ([{{LEADING | TRAILING | BOTH}
         [trim_character]
       | trim_character
       }
       the FROM
     ]
     trim_source
    )

The syntax in the above quote from Oracle 10gR2 official document: http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/img_text/trim.htm
single from this syntax definition we can see, small small TRIM function contains a more customizable features. 11 shows, for reference.

2. The simplest use start
without any parameters:
sec @ ora10g > the SELECT the TRIM ( 'SECOOLER') "the TRIM EG" from Dual;

TRIM e.g
--------
SECOOLER

This is the most common method of use, use the default parameters, the default will delete spaces before and after the TRIM while in the case of the string.

3. In fact, a first conventional method is equivalent to having the following wording "BOTH" parameter
sec @ ora10g > SELECT TRIM (both from 'SECOOLER') "the TRIM EG" from Dual;

TRIM e.g
--------
SECOOLER

"BOTH" before and after the string parameter represents the simultaneous removal of specified content (remove spaces by default).

4. Since the trial BOTH parameters, let's look at "TRAILING" and "LEADING" parameters Effect
sec @ ora10g > the SELECT the TRIM (trailing from 'SECOOLER') "the TRIM EG" from Dual;

TRIM e.g.
------------
    SECOOLER

sec@ora10g> select trim (leading from '    SECOOLER    ') "TRIM e.g." from dual;

TRIM e.g.
------------
SECOOLER

Visible, use the "TRAILING" parameter string trailing spaces can complete the delete function; and "LEADING" parameter on the contrary, to complete the string head space delete function.
In other words, use the "TRAILING" and "LEADING" parameter to specify the location to delete spaces.

5. "trim_character" parameter on stage
this parameter changes the "delete spaces" default behavior.
If you want to delete the string 'xxxxSECOOLERxxxx' "x" that appears before and after, "trim_character" parameter comes in handy.
@ ora10g sec > SELECT TRIM ( 'X' from 'xxxxSECOOLERxxxx') "the TRIM EG" from Dual;

TRIM e.g
--------
SECOOLER

With "BOTH", "TRAILING" and "LEADING" effect following three parameters, and the like prior to presentation. Look at the results, do not repeat them.
@ ora10g sec > SELECT TRIM (both 'X' from 'xxxxSECOOLERxxxx') "the TRIM EG" from Dual;

TRIM e.g
--------
SECOOLER

sec@ora10g> select trim (trailing 'x' from 'xxxxSECOOLERxxxx') "TRIM e.g." from dual;

TRIM e.g.
------------
xxxxSECOOLER

sec@ora10g> select trim (leading 'x' from 'xxxxSECOOLERxxxx') "TRIM e.g." from dual;

TRIM e.g.
------------
SECOOLERxxxx

Local 6. It should be noted
here "trim_character" parameter allows only one character, does not support multiple characters.
Given information is as follows:
sec @ ora10g > SELECT TRIM (leading 'XY' from 'xyxxSECOOLERxyyx') "the TRIM EG" from Dual;
SELECT TRIM (leading 'XY' from 'xyxxSECOOLERxyyx') "the TRIM EG" from Dual
       *
ERROR AT Line. 1 :
ORA-30001: Should the SET have have only the TRIM One Character

Since TRIM does not meet our requirements delete the string only "SECOOLER", there are other means to do it? of course there is. We use the RTRIM and LTRIM "chain fist" to complete this task.
1) Use RTRIM
sec @ ora10g > SELECT RTRIM ( 'xyxxSECOOLERxyyx', 'XY') "EG" from Dual;

e.g.
------------
xyxxSECOOLER

2)使用LTRIM
sec@ora10g> select ltrim('xyxxSECOOLERxyyx','xy') "e.g." from dual;

eg
------------
SECOOLERxyyx

3) and combined use LTRIM function RTRIM our purposes
sec @ ora10g > SELECT LTRIM (RTRIM ( 'xyxxSECOOLERxyyx', 'XY'), 'XY') "EG" from Dual;

eg
--------
SECOOLER

Considerations when using the RTRIM and LTRIM function: "xy" does not mean the whole "xy" strings match, but found that any of the characters "x" or character "y" all do delete operation.

7. Summary
feeling the Oracle function brings convenience, but is recommended for each function are commonly used to trace the origin to explore, maybe after trying you'll find: Oh, we often use these methods just drop in the ocean its real function

 

Transfer from: http: //www.2cto.com/database/201208/147087.html

Guess you like

Origin www.cnblogs.com/xckai/p/11411521.html