SQL RTRIM函数

SQL RTRIM函数

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

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);

RTRIM字符串函数的示例

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

SELECT RTRIM(  'JAVATPOINT           ','  ');

输出:

'JAVATPOINT'

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

SELECT RTRIM(  '              JAVATPOINT           ');

输出:

'              JAVATPOINT'

示例3:以下SELECT查询从指定的字符串中删除印度首都子字符串:

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(  '2021JavaTpoint2021', '2021');

输出:

2021JavaTpoint

示例6:下面的SELECT查询从指定的字符串中删除给定的一组数字:

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

输出:

JavaTpoint

示例7:以下SELECT查询将删除出现在已修整字符串中的字符串右侧的所有数字:

SELECT RTRIM(  'JavaTpoint90287', '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)
);  

以下是创建 Faculty_Info 表的 CREATE 语句:

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查询将大学教员的记录插入到 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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程