當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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

相關用法


注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 RTRIM() Function in MySQL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。