MySQL 中的 RTRIM()函数
RTRIM() :
MySQL 中的函数用于从字符串中删除尾随空格。
句法 :
RTRIM(str)
范围 :
RTRIM()函数接受一个如上所述和下面描述的参数。
- str –我们要从中删除尾随空格的字符串。
回报:
它在截断所有尾随空格后返回一个字符串。
示例-1:
使用 RTRIM函数删除给定字符串的所有尾随空格。
SELECT RTRIM ('geeksforgeeks')
AS RightTrimmedString;
输出 :
+----------------------+--------------------+
| geeksforgeeks | RightTrimmedString |
+----------------------+--------------------+
| geeksforgeeks | geeksforgeeks |
+----------------------+--------------------+
示例 2 :
使用 RTRIM函数删除给定字符串的所有尾随空格。
SELECT 'MySQL' AS String, RTRIM ('MySQL')
AS Tstring;
输出 :
+----------------------+---------+
| String | Tstring |
+----------------------+---------+
| MySQL | MySQL |
+----------------------+---------+
示例 3 :
RTRIM函数也可用于删除列数据的所有尾随空格。为了演示创建一个名为 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 )
);
向学生表插入一些数据:
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') ;
因此,学生表如下。
mysql> select * from Student;
输出 :
+------------+----------------------+---------------+
| 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 |
+------------+----------------------+---------------+
现在,我们将从 Student_name 列中删除所有尾随空格。
SELECT
Student_id, Student_name,
RTRIM( Student_name) AS TrimmedSname
FROM Student ;
输出 :
+------------+----------------------+-----------------+
| 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 |
+------------+----------------------+-----------------+