RTRIM (Transact-SQL)

截断所有尾随空格后返回一个字符串。

主题链接图标Transact-SQL 语法约定

语法

RTRIM ( character_expression )

参数

  • character_expression
    字符数据表达式。character_expression可以是常量、变量,也可以是字符列或二进制数据列。

    character_expression 的数据类型必须可隐式转换为 varchar。否则,请使用 CAST 显式转换 character_expression

返回类型

varchar 或nvarchar

注释

兼容级别可能影响返回值。有关详细信息,请参阅 sp_dbcmptlevel (Transact-SQL)

示例

A. 简单示例

以下示例将接受一个在句子结尾包含空格的字符串,并返回在句子结尾没有空格的文本。

SELECT RTRIM('Removes trailing spaces.   ');

下面是结果集:

-------------------------------------------------------------------------

Removes trailing spaces.

B. 将 RTRIM 用于变量

以下示例显示如何使用 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.

(1 row(s) affected)

-------------------------------------------------------------------------

Four spaces are after the period in this sentence. Next string.

(1 row(s) affected)