In MySQL, the RTRIM() function trims whitespace from the right-hand side of a string.
Syntax
The syntax goes like this:
RTRIM(str)
Where
str
is the string to trim.Example
Here’s a basic example of removing whitespace from the end of a string:
SELECT RTRIM('Rattlesnake ') AS Trimmed;
Result:
+-------------+ | Trimmed | +-------------+ | Rattlesnake | +-------------+
The trailing space is removed.
To demonstrate the effect better, here’s what it looks like when compared to the untrimmed string:
SELECT 'Rattlesnake ' AS Untrimmed, RTRIM('Rattlesnake ') AS Trimmed;
Result:
+--------------+-------------+ | Untrimmed | Trimmed | +--------------+-------------+ | Rattlesnake | Rattlesnake | +--------------+-------------+
If you look closely, you’ll see that the first one has extra space at the end and the second one doesn’t. This is even more clearly demonstrated in the next example.
Multiple Spaces
If you have multiple trailing spaces, all of them are trimmed:
SELECT 'Rattlesnake ' AS Untrimmed, RTRIM('Rattlesnake ') AS Trimmed;
Result:
+---------------------+-------------+ | Untrimmed | Trimmed | +---------------------+-------------+ | Rattlesnake | Rattlesnake | +---------------------+-------------+
Other Spaces
This function only trims trailing space. Any other space is left alone:
SELECT ' Crotalus triseriatus ' AS Untrimmed, RTRIM(' Crotalus triseriatus ') AS Trimmed;
Result:
+--------------------------------+---------------------------+ | Untrimmed | Trimmed | +--------------------------------+---------------------------+ | Crotalus triseriatus | Crotalus triseriatus | +--------------------------------+---------------------------+
In this example, space between each word is left intact, and leading space is also left intact on both strings. Only the the second string has its trailing space trimmed.
0 comments:
Post a Comment