w3resource

MySQL TRIM() function

TRIM() function

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

This function is useful in -

  • Removing leading and trailing spaces: Most commonly used to remove leading and trailing white spaces.
  • Data cleansing: TRIM() removes unwanted characters or white spaces from strings at the beginning and end.

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: 8.0

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 '.

Code:

SELECT TRIM(' trim '); 

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' ); 

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' ); 

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');
 

Output:

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

Video Presentation:

All String Functions (Slides presentation)

Previous: SUBSTRING() function
Next: UCASE() function



Follow us on Facebook and Twitter for latest update.