dcsimg
 

Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Tuesday Apr 14th 2020 by M S Sridhar

Language: SQL, Expertise: Intermediate: In MySQL, the TRIM function has additional implementations other than just trimming spaces. See what else you can do.

In MySQL, TRIM has additional implementations other than just trimming spaces. MySQL provided an overloaded version of TRIM function with which you can trim the LEADING, either side (BOTH) and also the TRAILING text.

Let's consider the following to understand how these behave.

Query:

SELECT TRIM(LEADING 'x' FROM 'xxxWHITExxx') AS TEXT;

Output: This will result in the character x being removed from the LEADING end.

+------------+
| TEXT       |
+------------+
| WHITExxx   |
+------------+

Query:

SELECT TRIM(BOTH 'x' FROM 'xxxWHITExxx') AS TEXT;

Output: This will result in the character x being removed from BOTH the ends.

+------------+
| TEXT       |
+------------+
| WHITE      |
+------------+

Query:

SELECT TRIM(TRAILING 'x' FROM 'xxxWHITExxx') AS TEXT;

Output: This will result in the character x being removed from the TRAILING end.

+------------+
| TEXT       |
+------------+
| xxxWHITE   |
+------------+
Home
Mobile Site | Full Site