Oracle trim, ltrim, rtrim function usage
This function has two functions:
First, remove spaces that everyone is familiar.
Example:
-- Trim removes leading and trailing spaces of a specified character
SQL> select trim ('dd df') from dual;
Trim ('dddf ')
------------
Dd DF
-- Ltrim removes spaces before a specified character
SQL> select ltrim ('dd df') from dual;
Ltrim ('dddf ')
-------------
Dd DF
-- Rtrim removes spaces after a specified character
SQL> select rtrim ('dd df') from dual;
Rtrim ('dddf ')
-------------
Dd DF
Second, remove the specified characters. Trim can only remove a single character, while ltrim and rtrim can remove multiple characters.
Trim character removal:
-- Remove the character string string2 from the front | back | followed by the character string1 (Leading | trailing | both). The default removal method is both.
Select trim (Leading | trailing | both string1 from string2) from dual;
Example:
SQL> select trim (Leading 'D 'from 'dfssa') from dual;
Trim (Leading 'D' from 'dfssa ')
---------------------------
Fssa
SQL> select trim (both '1' from '123sfd111') from dual;
Trim (both '1' from '123sfd111 ')
----------------------------
23sfd
SQL> select trim (trailing '2' from 'mongodsq12') from dual;
Trim (trailing '2' from 'processing dsq12'
------------------------------
213dsq1
Note: trim delimiters can only be single characters, as shown below. If the characters to be removed are character sets, an error is returned.
SQL> select trim (trailing '12' from '123dsq12') from dual;
Select trim (trailing '12' from '123dsq12 ') from dual
ORA-30001: Only one character in a collection
How to remove characters from ltrim and rtrim:
-- Indicates that string1 removes the character set matching string2. If no match exists, it returns
Select ltrim (string1, string2) from dual;
-- Rtrim is similar to ltrim, but it only removes the matching characters from the right.
Select rtrim (string1, string2) from dual;
Example:
-- As shown in the following figure, since the first letter from the right is a character that B does not match 'main', the returned result is still 'aaaminb'
SQL> select rtrim ('aaaaminb', 'main') from dual;
Rtrim ('aaaaminb', 'main ')
------------------------
Aaaaminb
-- The returned result is null as follows:
SQL> select rtrim ('aaaaminb', 'mainb') from dual;
Rtrim ('aaaaminb', 'mainb ')
-------------------------
SQL> select ltrim ('ccbcminb', 'cb ') from dual;
Ltrim ('ccbcminb', 'cb ')
----------------------
Minb