Tuesday, 10 July 2018

Mysql TRIM() function

Mysql 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
NameDescription
BOTHIndicates that prefixes from both left and right are to be removed.
LEADINGIndicates that only leading prefixes are to be removed.
TRAILINGIndicates that only trailing prefixes is to be removed.
remstrThe string to be removed.
FROMKeyword
strThe actual string from where remstr is to be removed.
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)
 

0 comments:

Post a Comment