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

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.