MySQL clears the space at the beginning and end of a string.

Source: Internet
Author: User

The space function at the beginning and end of a string in MySQL is a frequently used function. The space function at the beginning and end of a string in MySQL contains three types: ltrim (), rtrim (), trim (), next we will introduce you separately.

 
 
  1. mysql> select concat('.', ltrim(' ddd '), '.');  
  2. +----------------------------------+  
  3. | concat('.', ltrim(' ddd '), '.') |  
  4. +----------------------------------+  
  5. | .ddd .                           |  
  6. +----------------------------------+  
  7. mysql> select concat('.', rtrim(' ddd '), '.');  
  8. +----------------------------------+  
  9. | concat('.', rtrim(' ddd '), '.') |  
  10. +----------------------------------+  
  11. | . ddd.                           |  
  12. +----------------------------------+  
  13. mysql> select concat('.', trim(' ddd '), '.');  
  14. +---------------------------------+  
  15. | concat('.', trim(' ddd '), '.') |  
  16. +---------------------------------+  
  17. | .ddd.                           |  
  18. +---------------------------------+ 

The trim string function in MySQL is really powerful. It not only removes spaces at the beginning and end of a string, but also removes any specified characters. Ltrim (), rtrim () is only a subset of its functions. Let's take a look at the complete syntax of the trim function:

1. trim ([{both | leading | trailing} [remstr] from] str)
2. trim ([remstr from] str)

1. Clear the first character of the string.

 
 
  1. ysql> select trim(leading '.' from '..ddd..');  
  2. +----------------------------------+  
  3. | trim(leading '.' from '..ddd..') |  
  4. +----------------------------------+  
  5. | ddd..                            |  
  6. +----------------------------------+ 

2. Clear the tail character of the string.

 
 
  1. mysql> select trim(trailing '.' from '..ddd..');  
  2. +-----------------------------------+  
  3. | trim(trailing '.' from '..ddd..') |  
  4. +-----------------------------------+  
  5. | ..ddd                             |  
  6. +-----------------------------------+ 

3. Clear the first and end characters of the string.

 
 
  1. mysql> select trim(both '.' from '..ddd..');  
  2. +-------------------------------+  
  3. | trim(both '.' from '..ddd..') |  
  4. +-------------------------------+  
  5. | ddd                           |  
  6. +-------------------------------+  
  7. mysql> select trim('.' from '..ddd..');  
  8. +--------------------------+  
  9. | trim('.' from '..ddd..') |  
  10. +--------------------------+  
  11. | ddd                      |  
  12. +--------------------------+ 

Trim () removes spaces at the beginning and end of the string by default.

The preceding sections describe the space function at the beginning and end of a string in MySQL.

MySQL string case-sensitive Conversion Function

Use of MySQL concat Functions

Obtain the Automatically increasing MySQL primary key

MySQL primary key design principles

MySQL partition functions and restrictions

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.