×

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 Triggers

MySQL trigger is a function of the stored procedure to respond to the system program. This function responds and runs any data table event automatically. You can use it for...

5 minutes read.

MySQL Stored Procedure

MySQL creates the "stored procedure" function to operate database information. You can use parameters, blocks, and statements to create a new procedure. The procedure requires a database table to use...

3 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 Show index

This query helps to access and retrieve table information. This syntax displays the required index or all indexes of the table. It also displays the index type as per the...

3 minutes read.

MySQL variable

The MySQL variable is essential for storing data in the table. The variable declares data with a specific name or label to avoid confusion. This data label is used in...

6 minutes read.

MySQL CONCAT() function

In this context, we will learn how we can use the MySQL CONCAT() function with proper syntax and good examples. Introduction of MySQL CONCAT() function By using the MySQL CONCAT function, we...

3 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 minutes read.

MySQL POWER() function

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 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 function

MySQL function is a piece of program to perform some specific task. Here we pass a parameter and then return a single value. MySQL function mainly supports string, numeric, conditional,...

9 minutes read.

MySQL LN() function

In this context, we will learn how we can use the MySQL LN() function with proper syntax and good examples. Introduction of MySQL LN() function For evaluation of the natural logarithm of...

2 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 IF statement

The IF statement shows the true condition of the control flow function. The first condition is necessary to fulfill the requirement. The other condition is optional in the "IF" statement....

3 minutes read.

MySQL Union

MySQL contains the same categories of data in a different table. Sometimes, you require multiple tables to collect data together. MySQL union is a function to combine two table's data...

5 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 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 Operators

MySQL Operators MySQL operator needs advanced operation on the table and its data. This operator works with the "WHERE" clause. MySQL operators are a statement to modify information. It helps to...

13 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 Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 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.