Skip to content

TRIM()

Function Description

The TRIM() function returns a string that deletes unwanted characters.

You can explicitly indicate that the TRIM() function is removed from the string by using the LEADING, TRAILING or BOTH options.

If you do not specify anything, the TRIM() function uses the BOTH option by default.

[remstr] is the string to be deleted. The default is a space. That is, if a specific string is not specified, the TRIM() function only removes spaces.

Function Syntax

> TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

Parameter definition

Parameters Description
str Required parameters. It is to delete the sub-character remstr string.

Example

mysql> select trim(' abc '), trim('abc '), trim('abc'), trim('abc');
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| trim(abc ) | trim(abc ) | trim(abc) | trim(abc) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| abc | abc | abc | abc |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

drop table if exists t1;
create table t1(a varchar(100), b varchar(100));
insert into t1 values('abc', 'abc');
insert into t1 values('ah abc', 'ah abc');
insert into t1 values('aaao', 'o');
insert into t1 values('aaao', 'aa');
insert into t1 values('aaao', 'oaa');
mysql> select trim(both a from b) from t1;
+--------------------------+
| trim(both a from b) |
+--------------------------+
| |
| |
| o |
| Ah |
| o Ah |
+--------------------------+
5 rows in set (0.00 sec)

mysql> select trim(leading a from b) from t1;
+-----------------------------+
| trim(leading a from b) |
+-----------------------------+
| |
| |
| o |
| Ah |
| o Ah |
+-----------------------------+
5 rows in set (0.01 sec)

mysql> select trim(trailing a from b) from t1;
+------------------------------+
| trim(trailing a from b) |
+------------------------------+
| |
| |
| o |
| Ah |
| o Ah |
+------------------------------+
5 rows in set (0.00 sec)