MySQL has a TRIM() function that enables you to remove leading and/or trailing whitespace from a string.
You can also use it to remove other specified characters from either side (or both sides) of the string.
This article focuses on removing whitespace from both sides of the string.
Example
Here’s a basic example of removing whitespace from both sides of a string:
SELECT TRIM(' Joe Satriani ') AS Trimmed;
Result:
+--------------+ | Trimmed | +--------------+ | Joe Satriani | +--------------+
So, the string is stripped of any whitespace on both sides of the string. Note that any whitespace that’s not at the immediate start or end of the string remains.
Here’s what it looks like when compared to the untrimmed string:
SELECT TRIM(' Joe Satriani ') AS Trimmed, ' Joe Satriani ' AS Untrimmed;
Result:
+--------------+------------------------+ | Trimmed | Untrimmed | +--------------+------------------------+ | Joe Satriani | Joe Satriani | +--------------+------------------------+
Other Uses of TRIM()
Also, you aren’t just limited to spaces.
TRIM()
can be used to remove other characterstoo.
0 comments:
Post a Comment