RTRIM (Transact-SQL)
Applies to:
SQL Server (all supported versions)
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
Returns a character string after truncating all trailing spaces.
Transact-SQL Syntax Conventions
Syntax
RTRIM ( character_expression )
Note
To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation.
Arguments
character_expression
Is 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.
Return Types
varchar or nvarchar
Examples
A. Simple Example
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. ');
Here is the result set.
Removes trailing spaces.
B: Simple Example
The following example demonstrates how to use RTRIM to remove trailing spaces. This time there is another string concatenated to the first string to show that the spaces are gone.
SELECT RTRIM('Four spaces are after the period in this sentence. ') + 'Next string.';
Here is the result set.
Four spaces are after the period in this sentence.Next string.
C. Using RTRIM 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
Here is the result set.
Four spaces are after the period in this sentence. Next string.
Four spaces are after the period in this sentence. Next string.
See Also
LEFT (Transact-SQL)
LTRIM (Transact-SQL)
RIGHT (Transact-SQL)
STRING_SPLIT (Transact-SQL)
SUBSTRING (Transact-SQL)
TRIM (Transact-SQL)
CAST and CONVERT (Transact-SQL)
Data Types (Transact-SQL)
String Functions (Transact-SQL)