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:
JAVATPOINT | RightTrimmedString |
JAVATPOINT | JAVATPOINT |
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:
String | Tstring |
MySQL | MySQL |
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_id | Pupil_name | Pupil_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 Pupil_name column.
SELECT
Pupil_id, Pupil_name,
RTRIM( Pupil_name) AS TrimmedSname
FROM Pupil ;
Output:
Pupil_id | Pupil_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 |
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.