Open In App

RTRIM() Function in MySQL

Last Updated : 06 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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    |
+------------+----------------------+-----------------+

Previous Article
Next Article

Similar Reads

PLSQL | RTRIM Function
The PLSQL RTRIM function is used for removing all specified characters from the right-hand side of a string. The PLSQL RTRIM function accepts two parameters which are input_string and trim_string. If the user does not specify trim_string, it defaults to a single blank. If char is a character literal, then you must enclose it in single quotes. Oracl
2 min read
LTrim() and RTrim() Function in MS Access
1. LTrim() Function : In MS Access LTrim() function remove all the leading spaces from the given string. In LTrim() function a string will be pass as a parameter and it will return the string with no leading spaces. Syntax : LTrim(string) Example-1 : SELECT LTrim(" GEEKSFORGEEKS") AS LeftTrimmedString; Output - LeftTrimmedString GEEKSFORGEEKS Examp
1 min read
RTRIM() Function in SQL Server
The RTRIM() function removes trailing spaces from a string. Syntax : RTRIM(input_string) Parameter : RTRIM() function accepts single-parameter like input_string. input_string : It is an expression of character or binary data. It can be a literal string, variable, or column. Returns - It returns a string after truncating all trailing blanks. Example
1 min read
SQL RTRIM() Function
SQL RTRIM function is a string function that removes spaces from the right side of a character or string. RTRIM or Right Trim function is used in data cleaning and manipulation in SQL. In this example, we will learn the basics of the RTRIM function, and learn how it works with examples of different use cases. RTRIM() Function in SQLSQL RTRIM() func
3 min read
RPAD () and RTRIM() in MariaDB
1. RPAD Function : In MariaDB, The RPAD Function a string that is right-padded with a specified string to a certain length. In this function, the first parameter will be the string and the second parameter will be the length and the third parameter will be padding string. This function returns right-padded with a specified string to a given length.
2 min read
MySQL | Common MySQL Queries
MySQL server is a open-source relational database management system which is a major support for web based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. Even all social networking websites mainly Facebook, Twitter, and Google depends on MySQL dat
9 min read
MySQL | LEAD() and LAG() Function
The LEAD() and LAG() function in MySQL are used to get preceding and succeeding value of any row within its partition. These functions are termed as nonaggregate Window functions. The Window functions are those functions which perform operations for each row of the partition or window. These functions produce the result for each query row unlikely
3 min read
MySQL | BIN() Function
The BIN() function in MySQL is used to convert a decimal number to its binary equivalent.The BIN() function is equivalent to the CONV() function written in the format CONV(number,10,2). In this format CONV() function converts the number 'number' from base 10(decimal) to base 2(binary). It is important to note that the BIN() function can only be use
1 min read
MySQL | LAST_DAY() Function
The LAST_DAY() function in MySQL can be used to know the last day of the month for a given date or a datetime. The LAST_DAY() function takes a date value as argument and returns the last day of month in that date. The date argument represents a valid date or datetime. Syntax: LAST_DAY( Date ); If the date or datetime value is invalid, the function
2 min read
CURRENT_TIME() function in MySQL
CURRENT_TIME() function in MySQL is used to check the current time. It returns the current time as a value in 'hh:mm:ss' or hhmmss format, depending on whether the function is used in string or numeric context. Syntax : CURRENT_TIME OR CURRENT_TIME(precision) Parameter : This method accepts one parameter. precision - It specifies the returned fract
2 min read
Article Tags :