×

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples.

Introduction of MySQL RTRIM() function

It is the function in MySQL that is used to remove trailing spaces from a string.

Syntax of the MySQL RTRIM() function

The syntax of the MySQL RTRIM() function is given as follows:

RTRIM(str)

Parameters or arguments used in MySQL RTRIM() function:

There is only one parameter accepted by the RTRIM() function in MySQL, which is given as follows:

Str: The string from which we want to remove trailing spaces.

Returns:

It will return a string after truncating all trailing spaces.

Application used for RTRIM() function:

The RTRIM() function can be used in the given below MySQL versions.:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

Examples of MySQL RTRIM() function:

Now we will look into some MySQL RTRIM() function examples and will explore how we can use the RTRIM function in MySQL.

Example-1:

In this example, we will Remove all the trailing spaces of a given string using the RTRIM Function in MySQL.

SELECT RTRIM ('JAVATPOINT')
AS RightTrimmedString;

Output:

JAVATPOINTRightTrimmedString
JAVATPOINTJAVATPOINT
1 row in set (0.00 sec)

Example-2:

In this example, we will Remove all the trailing spaces of a given string using the RTRIM Function in MySQL.

SELECT 'MySQL' AS String, RTRIM ('MySQL')
AS Tstring;

Output:

StringTstring
MySQLMySQL
1 row in set (0.00 sec)

Example-3:

When we are required to remove all the trailing spaces of column data, we can use the RTRIM function's help. To demonstrate, create a table named Pupil.

CREATE TABLE Pupil
(
Pupil_id INT AUTO_INCREMENT,
Pupil_name VARCHAR(100) NOT NULL,
Pupil_Class VARCHAR(20) NOT NULL,
PRIMARY KEY(Pupil_id )
);

Inserting some data to the Pupil table.

INSERT INTO Pupil
(Pupil_name, Pupil_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 Pupil Table is as follows.

mysql> select * from Pupil;

Output:

Pupil_idPupil_namePupil_Class
1Ananya MajumdarIX
2Anushka SamantaX
3Aniket SharmaXI
4Anik DasX
5Riya JainIX
6Tapan SamantaX
7Deepak SharmaX
8Ankana JanaXII
9Shreya GhoshX

Now, we are going to remove all trailing spaces from the Pupil_name column.

SELECT
Pupil_id, Pupil_name,
RTRIM( Pupil_name) AS TrimmedSname
FROM Pupil ;

Output:

Pupil_idPupil_nameTrimmedSname
1Ananya MajumdarAnanya Majumdar
2Anushka SamantaAnushka Samanta
3Aniket SharmaAniket Sharma
4Anik DasAnik Das
5Riya JainRiya Jain
6Tapan SamantaTapan Samanta
7Deepak SharmaDeepak Sharma
8Ankana JanaAnkana Jana
9Shreya GhoshShreya Ghosh

Application of MySQL RTRIM() function:

This function is used to remove trailing spaces from a string.

Summary:

In the above context, we have learned how we can use the RTRIM() function in MySQL used to remove trailing spaces from a string.


Related Topics

MySQL Advance function

The advance function operates numerical values, string values, and data types.  The advance function converts, displays, and compares given values as per requirement. Here, you find out database, table, and...

4 minutes read.

MySQL Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL ROUND() function

In this context, we will learn how we can use the MySQL ROUND() function with proper syntax and good examples. Introduction of MySQL ROUND() function The ROUND() function in MySQL is used...

3 minutes read.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL MONTHNAME() function

In this context, we will learn how we can use the MySQL MONTHNAME() Function with proper syntax and good examples. Introduction of MySQL MONTHNAME() function In MySQL the MONTHNAME() Function is used...

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

3 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 minutes read.

MySQL Character Length Function

In this context, we will learn how we can use the MySQL CHARACTER_LENGTH() function with proper syntax and good examples. Introduction of MySQL CHARACTER_LENGTH() function CHARACTER_LENGTH() function in MySQL is used to...

2 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL Vs MongoDB

What is MongoDB? MongoDB is a distributed, open-source, cross-platform document-based database which was created to scale and develop applications simply. It was created as a NoSQL database by MongoDB Inc. MongoDB gets...

6 minutes read.

MySQL Command line client Basic Queries

MySQL – command line client Basic Queries There are many queries to work with the MySQL command-line client. Today, we will explore MySQL basic queries and their function. The MySQL query...

8 minutes read.

MySQL FORMAT() function

In this context, we will learn how we can use the MySQL FORMAT() function with proper syntax and good examples. Introduction of MySQL FORMAT() function This function in MySQL helps to format...

2 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

3 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays...

4 minutes read.

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values,...

2 minutes read.

MySQL EQUI JOIN

An equijoin is an operation that combines multiple tables based on equality or matching column values in the associated tables. This operation links more than two tables based on a...

5 minutes read.