SQL Tutorial SQL Advanced SQL Database SQL References

MySQL RTRIM() Function



The MySQL RTRIM() function removes all space characters from the right-hand side of a string.

Syntax

RTRIM(string)

Parameters

string Required. Specify the string to trim the space characters from the right-hand side.

Return Value

Returns the trimmed version of the specified string.

Example 1:

The example below shows the usage of RTRIM() function.

mysql> SELECT RTRIM('SQL Tutorial    ');
Result: 'SQL Tutorial'

mysql> SELECT RTRIM('  SQL Tutorial    ');
Result: '  SQL Tutorial'

mysql> SELECT RTRIM('Learning  SQL  is  fun.    ');
Result: 'Learning  SQL  is  fun.'

mysql> SELECT RTRIM('  Learning  SQL  is  fun.    ');
Result: '  Learning  SQL  is  fun.'

Example 2:

Consider a database table called Employee. When the following INSERT statements are executed, the Name column will contain records with trailing spaces.

INSERT INTO Employee VALUES ('John        ', 'London', 3000);
INSERT INTO Employee VALUES ('Marry        ', 'New York', 2750);
INSERT INTO Employee VALUES ('Jo      ', 'Paris', 2800);
INSERT INTO Employee VALUES ('Kim      ', 'Amsterdam', 3100);

-- see the result
SELECT * FROM Employee;

The query will produce the following result:

NameCitySalary
John        London3000
Marry        New York2750
Jo      Paris2800
Kim      Amsterdam3100

To remove the trailing spaces from the Name column of the Employee table, the following query can be used:

UPDATE Employee SET Name = RTRIM(Name);

-- see the result
SELECT * FROM Employee;

This will produce the following result:

NameCitySalary
JohnLondon3000
MarryNew York2750
JoParis2800
KimAmsterdam3100

❮ MySQL Functions