适用于:SQL Server
Azure SQL 数据库
Azure SQL 托管实例
Azure Synapse Analytics
Microsoft Fabric中的SQL分析端点
Microsoft Fabric中的仓库
Microsoft Fabric中的SQL数据库
截断所有尾随空格后返回一个字符串。
删除字符串结尾的空格字符 char(32) 或其他指定字符。
语法
SQL Server 2022 (16.x) 之前的 SQL Server 语法:
RTRIM ( character_expression )
SQL Server 2022 (16.x) 及更高版本、Azure SQL 托管实例、Azure SQL 数据库、Azure Synapse Analytics 和 Microsoft Fabric 的语法:
重要
数据库兼容性级别必须设置为 160 或更高版本才能使用可选 字符 参数。
RTRIM ( character_expression , [ characters ] )
参数
character_expression
字符或二进制数据的表达式。 character_expression 可以是常量、变量或列。 character_expression 的数据类型必须可隐式转换为 varchar,text、ntext 和 image 除外。 否则,请使用 CAST 显式转换 character_expression。
characters
适用于:SQL Server 2022 (16.x) 及更高版本。
任何非 LOB 字符类型(nvarchar、varchar、nchar 或 char)的文本、变量或函数调用,其中包含应删除的字符。 不允许使用 nvarchar(max) 和 varchar(max) 类型。
返回类型
返回一个字符串自变量类型的字符表达式,其中已从 character_expression 末尾删除空格字符 char(32) 或其他指定字符。 如果输入字符串是 NULL,则返回 NULL。
注解
若要启用可选 字符 位置参数,请对执行查询时连接到的数据库启用数据库兼容性级别 160 。
示例
A. 删除尾随空格
以下示例将接受一个在句子结尾包含空格的字符串,并返回在句子结尾没有空格的文本。
SELECT RTRIM('Removes trailing spaces. ');
结果集如下。
Removes trailing spaces.
B. 使用变量删除尾随空格
以下示例显示如何使用 RTRIM 删除字符变量中的尾随空格。
DECLARE @string_to_trim VARCHAR(60);
SET @string_to_trim = 'Four spaces are after the period in this sentence. ';
SELECT @string_to_trim + ' Next string.';
SELECT RTRIM(@string_to_trim) + ' Next string.';
GO
结果集如下。
Four spaces are after the period in this sentence. Next string.
Four spaces are after the period in this sentence. Next string.
°C 删除字符串末尾的指定字符
重要
你将需要将数据库兼容性级别设置为 160 才能使用可选 characters 自变量。
以下示例删除 abc. 字符串末尾的字符 .123abc.。
SELECT RTRIM('.123abc.' , 'abc.');
结果集如下。
.123