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.
- mysql> select concat('.', ltrim(' ddd '), '.');
- +----------------------------------+
- | concat('.', ltrim(' ddd '), '.') |
- +----------------------------------+
- | .ddd . |
- +----------------------------------+
- mysql> select concat('.', rtrim(' ddd '), '.');
- +----------------------------------+
- | concat('.', rtrim(' ddd '), '.') |
- +----------------------------------+
- | . ddd. |
- +----------------------------------+
- mysql> select concat('.', trim(' ddd '), '.');
- +---------------------------------+
- | concat('.', trim(' ddd '), '.') |
- +---------------------------------+
- | .ddd. |
- +---------------------------------+
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.
- ysql> select trim(leading '.' from '..ddd..');
- +----------------------------------+
- | trim(leading '.' from '..ddd..') |
- +----------------------------------+
- | ddd.. |
- +----------------------------------+
2. Clear the tail character of the string.
- mysql> select trim(trailing '.' from '..ddd..');
- +-----------------------------------+
- | trim(trailing '.' from '..ddd..') |
- +-----------------------------------+
- | ..ddd |
- +-----------------------------------+
3. Clear the first and end characters of the string.
- mysql> select trim(both '.' from '..ddd..');
- +-------------------------------+
- | trim(both '.' from '..ddd..') |
- +-------------------------------+
- | ddd |
- +-------------------------------+
- mysql> select trim('.' from '..ddd..');
- +--------------------------+
- | trim('.' from '..ddd..') |
- +--------------------------+
- | ddd |
- +--------------------------+
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