MySQL RTRIM() Function
The MySQL RTRIM() function is used to remove all the trailing spaces from the given string. It accepts a string value as a parameter and returns the result by removing any whitespace characters, such as spaces, tabs, or newlines, from the right end of the string.
Syntax
Following is the syntax of MySQL RTRIM() function −
RTRIM(str)
Parameters
This function takes a string value as a parameter.
Return Value
This function returns the modified string with trailing spaces removed.
Example
In the following example, we are using the RTRIM() function to remove the trailing spaces from the string ' Tutorialspoint ' −
SELECT RTRIM(' Tutorialspoint ');
Following is the output of the above code −
| RTRIM(' Tutorialspoint ') |
|---|
| Tutorialspoint |
Example
You can also pass numerical values as arguments to this function by converting them to strings −
SELECT RTRIM(' 5578 ');
The output obtained is as follows −
| RTRIM(' 5578 ') |
|---|
| 5578 |
Example
If the argument passed to this function is NULL, it returns NULL −
SELECT RTRIM(NULL );
We get the output as follows −
| RTRIM(NULL ) |
|---|
| 0x |
If you disable the --binary-as-hex value, the result of the above query will be as follows −
SELECT RTRIM(NULL );
We get the output as follows −
| RTRIM(NULL ) |
|---|
| NULL |
Example
You can also use this function to remove trailing spaces from the values of a column of a table.
Let us create a table named "PLAYERS_TABLE" and insert records into it using CREATE and INSERT statements as shown below −
CREATE TABLE PLAYERS_TABLE( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
Now, let us insert records into it using the INSERT statement −
INSERT INTO PLAYERS_TABLE VALUES
(1, 'Shikhar ', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'),
(2, 'Jonathan ', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'),
(3, 'Kumara ', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'),
(4, 'Virat ', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'),
(5, 'Rohit ', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'),
(6, 'James ', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
The PLAYERS_TABLE obtained is as follows −
| ID | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country |
|---|---|---|---|---|---|
| 1 | Shikhar | Dhawan | 1981-12-05 | Delhi | India |
| 2 | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica |
| 3 | Kumara | Sangakkara | 1977-10-27 | Matale | Srilanka |
| 4 | Virat | Kohli | 1988-11-05 | Delhi | India |
| 5 | Rohit | Sharma | 1987-04-30 | Nagpur | India |
| 6 | James | Anderson | 1982-06-30 | Burnley | England |
Following query removes all the trailing spaces entities of the values of the column First_Name −
SELECT ID, First_Name, Last_Name, Country, RTRIM(First_Name) FROM PLAYERS_TABLE;
Output
After executing the above code, we get the following output −
| ID | First_Name | Last_Name | Country | RTRIM(First_Name) |
|---|---|---|---|---|
| 1 | Shikhar | Dhawan | India | Shikhar |
| 2 | Jonathan | Trott | SouthAfrica | Jonathan |
| 3 | Kumara | Sangakkara | Srilanka | Kumara |
| 4 | Virat | Kohli | India | Virat |
| 5 | Rohit | Sharma | India | Rohit |
| 6 | James | Anderson | England | James |
Example
Additionally, you can use the RTRIM() function along with the UPDATE statement to remove trailing spaces in a column.
In the following example, we are updating the 'First_Name' column in the 'PLAYERS_TABLE' −
UPDATE PLAYERS_TABLE SET First_name = RTRIM(First_name);
Output
Following is the output obtained −
Query OK, 6 rows affected (0.02 sec) Rows matched: 6 Changed: 6 Warnings: 0
Verification
You can verify the changes done using the following SELECT query −
SELECT * FROM PLAYERS_TABLE;
The result produced is as follows −
| ID | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country |
|---|---|---|---|---|---|
| 1 | Shikhar | Dhawan | 1981-12-05 | Delhi | India |
| 2 | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica |
| 3 | Kumara | Sangakkara | 1977-10-27 | Matale | Srilanka |
| 4 | Virat | Kohli | 1988-11-05 | Delhi | India |
| 5 | Rohit | Sharma | 1987-04-30 | Nagpur | India |
| 6 | James | Anderson | 1982-06-30 | Burnley | England |
Example
We can also use the RTRIM() function along with WHERE clause.
Here, we are removing the trailing spaces for the player names whose country is not "INDIA" −
UPDATE PLAYERS_TABLE SET First_name = RTRIM(First_name) WHERE Country <> 'India';
Output
Following is the output obtained −
Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0
Verification
You can verify the changes done using the following SELECT query −
SELECT * FROM PLAYERS_TABLE;
The table obtained is as follows −
| ID | First_Name | Last_Name | Date_Of_Birth | Place_Of_Birth | Country |
|---|---|---|---|---|---|
| 1 | Shikhar | Dhawan | 1981-12-05 | Delhi | India |
| 2 | Jonathan | Trott | 1981-04-22 | CapeTown | SouthAfrica |
| 3 | Kumara | Sangakkara | 1977-10-27 | Matale | Srilanka |
| 4 | Virat | Kohli | 1988-11-05 | Delhi | India |
| 5 | Rohit | Sharma | 1987-04-30 | Nagpur | India |
| 6 | James | Anderson | 1982-06-30 | Burnley | England |