Oracle trim, ltrim, rtrim function usage

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.