RTRIM() Function in MySQL

06 Oct, 2020
RTRIM() :It is the function in MySQL that is used to remove trailing spaces from a string.
Syntax :
RTRIM(str)
Parameter :RTRIM() function accepts one parameter as mentioned above and described below.
str –The string from which we want to remove trailing spaces.
Returns :It returns a string after truncating all trailing spaces.
Example-1 :Removing all the trailing spaces of a given string using RTRIM Function.
SELECT RTRIM ('geeksforgeeks') 
AS RightTrimmedString;  

Output :
+----------------------+--------------------+
| geeksforgeeks        | RightTrimmedString |
+----------------------+--------------------+
| geeksforgeeks        | geeksforgeeks      |
+----------------------+--------------------+

Example-2 :Removing all the trailing spaces of a given string using RTRIM Function.
SELECT 'MySQL' AS String, RTRIM ('MySQL') 
AS Tstring;

Output :
+----------------------+---------+
| String               | Tstring |
+----------------------+---------+
| MySQL                | MySQL   |
+----------------------+---------+

Example-3 :RTRIM Function can also be used to remove all the trailing spaces of column data. To demonstrate create a table named Student.
CREATE TABLE Student
(
  Student_id INT AUTO_INCREMENT,  
  Student_name VARCHAR(100) NOT NULL,
  Student_Class VARCHAR(20) NOT NULL,
  PRIMARY KEY(Student_id )

);

Inserting some data to the Student table :
INSERT INTO Student
(Student_name, Student_Class )
VALUES
  ('Ananya Majumdar     ', 'IX'),
  ('Anushka Samanta    ', 'X' ),
  ('Aniket Sharma   ', 'XI' ),
  ('Anik Das      ', 'X'  ),
  ('Riya Jain   ', 'IX' ),
  ('Tapan Samanta    ', 'X' ),
  ('Deepak Sharma    ', 'X'  ),
  ('Ankana Jana    ', 'XII'),
  ('Shreya Ghosh    ', 'X') ;

So, the Student Table is as follows.
mysql> select * from Student;

Output :
+------------+----------------------+---------------+
| Student_id | Student_name         | Student_Class |
+------------+----------------------+---------------+
|          1 | Ananya Majumdar      | IX            |
|          2 | Anushka Samanta      | X             |
|          3 | Aniket Sharma        | XI            |
|          4 | Anik Das             | X             |
|          5 | Riya Jain            | IX            |
|          6 | Tapan Samanta        | X             |
|          7 | Deepak Sharma        | X             |
|          8 | Ankana Jana          | XII           |
|          9 | Shreya Ghosh         | X             |
+------------+----------------------+---------------+

Now, we are going to remove all trailing spaces from the Student_name column.
  SELECT  
  Student_id,  Student_name,
  RTRIM( Student_name) AS  TrimmedSname 
  FROM Student ;   

Output :
+------------+----------------------+-----------------+
| Student_id | Student_name         | TrimmedSname    |
+------------+----------------------+-----------------+
|          1 | Ananya Majumdar      | Ananya Majumdar |
|          2 | Anushka Samanta      | Anushka Samanta |
|          3 | Aniket Sharma        | Aniket Sharma   |
|          4 | Anik Das             | Anik Das        |
|          5 | Riya Jain            | Riya Jain       |
|          6 | Tapan Samanta        | Tapan Samanta   |
|          7 | Deepak Sharma        | Deepak Sharma   |
|          8 | Ankana Jana          | Ankana Jana     |
|          9 | Shreya Ghosh         | Shreya Ghosh    |
+------------+----------------------+-----------------+

DBMS-SQL
mysql
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
Window functions in SQL
What is Temporary Table in SQL?
SQL using Python
SQL | Sub queries in From Clause
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
RANK() Function in SQL Server
SQL Query to Convert VARCHAR to INT
SQL Query to Compare Two Dates
SQL | DROP, TRUNCATE