TRIM() FUNCTION in MySQL

TRIM() FUNCTION
The MySQL TRIM function is used to remove all the specified characters either from the beginning or the end of a string or from both sides. The various versions of MySQL supports the TRIM function, namely, MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0 and MySQL 3.23.

Syntax:

TRIM ( LEADING | TRAILING | BOTH trim_string FROM string )

Parameters:
LEADING: It is an optional parameter which is a function to remove trim_string from the beginning of the string.
TRAILING: It is an optional parameter which is a function to remove trim_string from the end of the string.
BOTH: It is an optional parameter which is a function to remove trim_string from the beginning and the end of the string.
string: It is used to specify the string to trim characters from either or both sides.
trim_string: It is an optional parameter which is used to specify the string that will be trimmed or eliminated from the original or desired string. In case there is no value set for this parameter, it removes all the spaces from the string.

Example 1:

mysql> SELECT TRIM ( LEADING ‘abc’ FROM ‘abc_xyz_abc’ );

Output:

‘_xyz_abc’

Explanation:
The substring is removed from the beginning of the string.

Example 2:

mysql> SELECT TRIM ( TRAILING ‘abc’ FROM ‘abc_xyz_abc’ );

Output:

‘abc_xyz_’

Explanation:
The substring is removed from the end of the string.

Example 3:

mysql> SELECT TRIM ( BOTH ‘abc’ FROM ‘abc_xyz_abc’ );

Output:

‘_xyz_’

Explanation:
The substring is removed from both sides of the string.

Example 4:

mysql> SELECT TRIM ( ‘abc’ FROM ‘abc_xyz_abc’ );

Output:

‘_xyz_’

Explanation:
The substring is removed from both sides of the string by default.

Example 5:

mysql> SELECT TRIM ( ‘  abc_xyz_abc  ’ );

Output:

‘abc_xyz_abc’

Explanation:
All the space characters are removed from both sides of the string by default.

Please Share