Skip to content

Latest commit

 

History

History
150 lines (108 loc) · 5.77 KB

File metadata and controls

150 lines (108 loc) · 5.77 KB
title RTRIM (Transact-SQL)
description RTRIM (Transact-SQL)
author MikeRayMSFT
ms.author mikeray
ms.reviewer randolphwest
ms.date 08/22/2022
ms.prod sql
ms.prod_service database-engine, sql-database, synapse-analytics, pdw
ms.technology t-sql
ms.topic reference
f1_keywords
RTRIM_TSQL
RTRIM
helpviewer_keywords
RTRIM function
character strings [SQL Server], trailing blanks
blank characters [SQL Server]
trailing blanks
dev_langs
TSQL
monikerRange >= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || >= sql-server-linux-2017 || >= sql-server-ver15 || >= sql-server-ver16 || >= sql-server-linux-ver15 || >= sql-server-linux-ver16 || = azuresqldb-mi-current

RTRIM (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw]

::: moniker range="<=sql-server-ver15 || <=sql-server-linux-ver15 || = azure-sqldw-latest || = azuresqldb-current || = azuresqldb-mi-current" Returns a character string after truncating all trailing spaces. ::: moniker-end

::: moniker range=">=sql-server-ver16 || >=sql-server-linux-ver16" Removes space character char(32) or other specified characters from the end of a string. ::: moniker-end

:::image type="icon" source="../../database-engine/configure-windows/media/topic-link.gif" border="false"::: Transact-SQL Syntax Conventions

Syntax

::: moniker range="<=sql-server-ver15 || <=sql-server-linux-ver15 || = azure-sqldw-latest || = azuresqldb-current || = azuresqldb-mi-current"

RTRIM ( character_expression )

::: moniker-end

::: moniker range=">=sql-server-ver16 || >=sql-server-linux-ver16" Syntax for [!INCLUDE sssql22-md] and later:

RTRIM ( character_expression , [ characters ] )

Syntax for [!INCLUDE ssazure_md]:

RTRIM ( character_expression )

::: moniker-end

[!INCLUDEsql-server-tsql-previous-offline-documentation]

Arguments

character_expression

An expression of character data. character_expression can be a constant, variable, or column of either character or binary data.

character_expression must be of a data type that is implicitly convertible to varchar. Otherwise, use CAST to explicitly convert character_expression.

::: moniker range=">=sql-server-ver16 || >=sql-server-linux-ver16"

characters

Applies to: [!INCLUDE sssql22-md] and later.

A literal, variable, or function call of any non-LOB character type (nvarchar, varchar, nchar, or char) containing characters that should be removed. nvarchar(max) and varchar(max) types aren't allowed. ::: moniker-end

Return types

::: moniker range="<=sql-server-ver15 || <=sql-server-linux-ver15 || = azure-sqldw-latest || = azuresqldb-current || = azuresqldb-mi-current" varchar or nvarchar ::: moniker-end

::: moniker range=">=sql-server-ver16 || >=sql-server-linux-ver16" Returns a character expression with a type of string argument where the space character char(32) or other specified characters are removed from the end of a character_expression. Returns NULL if input string is NULL.

Remarks

To enable the optional characters positional argument, enable database compatibility level 160 on the database(s) that you are connecting to when executing queries. ::: moniker-end

Examples

A. Remove trailing spaces

The following example takes a string of characters that has spaces at the end of the sentence, and returns the text without the spaces at the end of the sentence.

SELECT RTRIM('Removes trailing spaces.   ');

[!INCLUDEssResult]

Removes trailing spaces.

B. Remove trailing spaces with a variable

The following example demonstrates how to use RTRIM to remove trailing spaces from a character variable.

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

[!INCLUDEssResult]

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

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

C. Remove specified characters from the end of a string

The following example removes the characters abc. from the end of the .123abc. string.

SELECT RTRIM('.123abc.' , 'abc.');

[!INCLUDEssResult]

.123

See also