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 are to be removed. |
remstr | The string to be removed. |
FROM | Keyword |
str | The actual string from where remstr is to be removed. |
Image Presentation
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 ‘);
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’.
- 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’.
- 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’.
- 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)