w3resource
MYSQL Tutorial

MySQL TRIM() function

TRIM() function

MySQL TRIM() function returns a string after removing all prefixes or suffixes from the given string.

Syntax:

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

Arguments

Name Description
BOTH Indicates that prefixes from both left and right are to be removed.
LEADING Indicates that only leading prefixes are to be removed.
TRAILING Indicates that only trailing prefixes is to be removed.
remstr The string to be removed.
FROM Keyword
str The actual string from where remstr is to be removed.

Syntax Diagram:

MySQL TRIM() Function - Syntax Diagram

MySQL Version: 5.6

Video Presentation

Pictorial Presentation

MySQL TRIM

Example : MySQL TRIM() function

The following MySQL statement returns the string after removing the leading and trailing spaces from the given string ' trim '.

SELECT TRIM(' trim '); 

Sample Output:

mysql> SELECT TRIM(' trim '); 
+----------------+
| TRIM(' trim ') |
+----------------+
| trim           | 
+----------------+
1 row in set (0.00 sec)

Example of MySQL TRIM() function to remove leading string

The following MySQL statement returns the string after removing the leading string 'leading' from the given string 'leadingtext'.

Code:

SELECT TRIM(LEADING 'leading' FROM 'leadingtext' ); 

Sample Output:

mysql> SELECT TRIM(LEADING 'leading' FROM 'leadingtext' ); 
+---------------------------------------------+
| TRIM(LEADING 'leading' FROM 'leadingtext' ) |
+---------------------------------------------+
| text                                        | 
+---------------------------------------------+
1 row in set (0.02 sec)

Example MySQL TRIM() function to remove trailing string

The following MySQL statement returns the string after removing the trailing string 'trailing' from the given string 'texttrailing'.

Code:

SELECT TRIM(TRAILING 'trailing' FROM 'texttrailing' ); 

Sample Output:

 mysql> SELECT TRIM(TRAILING 'trailing' FROM 'texttrailing' );
+------------------------------------------------+
| TRIM(TRAILING 'trailing' FROM 'texttrailing' ) |
+------------------------------------------------+
| text                                           | 
+------------------------------------------------+
1 row in set (0.00 sec)
 

Example MySQL TRIM() function removing from both side

The following MySQL statement returns the string after removing the leading and trailing string 'leadtrail' from the given string 'leadtrailtextleadtrail'.

Code:

SELECT TRIM(BOTH 'leadtrail' FROM 'leadtrailtextleadtrail');
 

Sample Output:

mysql> SELECT TRIM(BOTH 'leadtrail' FROM 'leadtrailtextleadtrail');
+------------------------------------------------------+
| TRIM(BOTH 'leadtrail' FROM 'leadtrailtextleadtrail') |
+------------------------------------------------------+
| text                                                 | 
+------------------------------------------------------+
1 row in set (0.00 sec)
 

All String Functions

MySQL String Functions, slide presentation