SQL RTRIM 函数

一、SQL RTRIM 函数 语法

SQL RTRIM 函数 从给定原始字符串的右侧截断给定字符或子字符串。它还截断指定字符串右侧的空格。

语法1:此语法使用带有 SQL 表列名的 RTRIM 函数:

SELECT RTRIM(Column_Name, string) AS Alias_Name FROM Table_Name;  

在语法中,我们必须指定要在其上运行 RTRIM 函数的列的名称。

语法2:此语法使用带有字符集(字符串)的 RTRIM 函数:

SELECT RTRIM(Original_String, trimmed_string);  

语法3:此语法使用带有单个字符的 RTRIM 函数:

SELECT RTRIM(Original_String, trimmed_character);  

二、SQL RTRIM 函数 示例

示例 1:以下 SELECT 查询根据 RTRIM 函数从指定字符串中截断给定空格:

SELECT RTRIM(  'YIIDIAN           ','  ');  

输出结果为:

'YIIDIAN'

示例 2:以下 SELECT 查询根据 RTRIM 函数从指定字符串中截断空格:

SELECT RTRIM(  '              YIIDIAN           ');  

输出结果为:

'              YIIDIAN'

示例 3:以下 SELECT 查询从指定字符串中修剪 CAPITAL OF INDIA 子字符串:

SELECT RTRIM(  'NEW DELHI IS THE CAPITAL OF INDIA', 'CAPITAL OF INDIA');  

输出结果为:

NEW DELHI IS THE

示例 4:以下 SELECT 查询从指定字符串中修剪给定符号:

SELECT RTRIM(  '####98221545###', '#');  

输出结果为:

####98221545

示例 5:以下 SELECT 查询从指定字符串中修剪给定的一组数字:

SELECT RTRIM(  '2021Yiidian2021', '2021');  

输出结果为:

2021Yiidian

示例 6:以下 SELECT 查询从指定字符串中修剪给定的一组数字:

SELECT RTRIM(  'Yiidian202120212021', '2021');  

输出结果为:

Yiidian

示例 7:以下 SELECT 查询从字符串右侧修剪修剪后的字符串中存在的所有数字:

SELECT RTRIM(  'Yiidian90287', '0123456789');  

此命令删除修剪后的字符串中单独出现的数字。

输出结果为:

JavaTpoint

示例 8:此示例对结构化查询语言中的表使用 RTRIM 函数。

在这个例子中,我们使用了下面的 Faculty_Info 表,它有助于理解 LTRIM 字符串函数。在 SQL 数据库中创建新表的语法如下:

CREATE TABLE table_name  
(  
1st_Column Data Type (character_size of 1st Column),    
2nd_Column Data Type (character_size of the 2nd column ),    
3rd_Column Data Type (character_size of the 3rd column),    
...    
  
Nth_Column Data Type (character_size of the Nth column)  
); 

以下 CREATE 语句创建Faculty_Info表:

CREATE TABLE Faculty_Info  
(  
Faculty_ID INT NOT NULL PRIMARY KEY,    
Faculty_First_Name VARCHAR (100),    
Faculty_Last_Name VARCHAR (100),    
Faculty_Dept_Id INT NOT NULL,  
Faculty_Address Varchar(120),  
Faculty_City Varchar (80),  
Faculty_Salary INT   
);  

下面的 INSERT 语句将大学 Faculties 的记录插入到Faculty_Info表中:

INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1001, 'Arush       ', 'Sharma       ', 4001, 'Aman Vihar       ', Delhi, 20000);  
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1002, 'Bulbul       ', 'Roy       ', 4002, 'Nirman Vihar       ', Delhi, 38000 );  
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1004, 'Saurabh       ', 'Sharma       ', 4001, 'Sector 128       ', Mumbai, 45000);  
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1005, 'Shivani       ', 'Singhania       ', 4001, 'Vivek Vihar       ', Kolkata, 42000);  
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary) VALUES (1006, 'Avinash       ', 'Sharma       ', 4002, 'Sarvodya Calony       ', Delhi, 28000);  
INSERT INTO Faculty_Info (Faculty_ID, Faculty_First_Name, Faculty_Last_Name Faculty_Dept_Id, Faculty_Address, Faculty_City, Faculty_Salary)VALUES (1007, 'Shyam       ', 'Besas       ', 4003, 'Krishna Nagar       ', Lucknow, 35000);  

以下 SELECT 语句显示上述Faculty_Info表的插入记录:

SELECT * FROM Faculty_Info;   

输出结果为:

Faculty_Id Faculty_First_Name Faculty_Last_Name Faculty_Dept_Id Faculty_Address Faculty_City Faculty_Salary
1001 'Arush ' 'Sharma ' 4001 'Aman Vihar ' Delhi 20000
1002 'Bulbul ' 'Roy ' 4002 'Nirman Vihar ' Delhi 38000
1004 'Saurabh ' 'Roy ' 4001 'Sector 128 ' Mumbai 45000
1005 'Shivani ' 'Singhania ' 4001 'Vivek Vihar ' Kolkata 42000
1006 'Avinash ' 'Sharma ' 4002 'Sarvodya Calony ' Delhi 28000
1007 'Shyam ' 'Besas ' 4003 'Krishna Nagar ' Lucknow 35000

以下 SELECT 查询将 RTRIM 函数与上述 Faculty_Info 表的 Faculty_Last_Name 列一起使用:

SELECT Faculty_Last_Name, RTRIM(Faculty_Last_Name) AS RTRIM_LastName FROM Faculty_Info;  

此 SQL 语句从右侧修剪空格:

Faculty_Last_Name RTRIM_LastName
'Sharma ' 'Sharma'
'Roy ' 'Roy'
'Roy ' 'Roy'
'Singhania ' 'Singhania'
'Sharma ' 'Sharma'
'Besas ' 'Besas'

以下 SELECT 查询将 RTRIM 函数与上面 Faculty_Info 表中的 Faculty_Id 大于 1002 的那些学院的 Faculty_First_Name 和 Faculty_Address 列一起使用:

SELECT Faculty_Id, RTRIM(Faculty_Last_Name), RTRIM(Faculty_Address) FROM Faculty_Info WHERE Faculty_Id >1002;  

此 SQL 语句从右侧修剪空间。

Faculty_Id RTRIM(Faculty_First_Name) RTRIM(Faculty_Address)
1004 'Saurabh' 'Sector 128'
1005 'Shivani' 'Vivek Vihar'
1006 'Avinash' 'Sarvodya Calony'
1007 'Shyam' 'Krishna Nagar'

热门文章

优秀文章