Common MySQL string processing functions

Source: Internet
Author: User

MySQL string processing functions are our most commonly used functions. The MySQL string processing functions described below are used to convert string cases and clear spaces at the beginning and end of a string, we hope that you will have a better understanding of MySQL string processing functions.

MySQL string case-sensitive Conversion

DeMySQL string case-sensitive conversion functions include lower (), uppper (), lcase (), and ucase ()

 
 
  1. mysql> select lower('DDD');  
  2. +--------------+  
  3. | lower('DDD') |  
  4. +--------------+  
  5. |   ddd |  
  6. +--------------+  
  7. mysql> select upper('ddd');    
  8. +--------------+  
  9. | upper('ddd') |  
  10. +--------------+  
  11. | DDD |  
  12. +--------------+  
  13. mysql> select lcase('DDD');    
  14. +--------------+  
  15. | lcase('DDD') |  
  16. +--------------+  
  17. | ddd |  
  18. +--------------+  
  19. mysql> select ucase('ddd');  
  20. +--------------+  
  21. | ucase('ddd') |  
  22. +--------------+  
  23. | DDD |  
  24. +--------------+  

In general, I select lower () and upper () to convert string case, because it is compatible with functions in other databases.

Removes spaces at the beginning and end of a string.

In MySQL, there are three spaces at the beginning and end of the MySQL string: ltrim (), rtrim (), trim ()

 
 
  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)  
  2.  

1. Clear the MySQL string header characters.

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

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. +-----------------------------------+  
  7.  

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 MySQL string by default.


 

MySQL string segmentation implementation

Replace string with the MySQL replace Function

How to quickly enable MySQL slow log query

How to query duplicate records in MYSQL

How to Implement MySQL full-text Query

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.