dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL
Expertise: Intermediate
Apr 14, 2020

Understanding the TRIM Function in MySQL

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   |
+------------+
M S Sridhar
 
Thanks for your registration, follow us on our social networks to keep up-to-date