Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate SQL, Power BI, Fabric, and AI community-led event. March 31 - April 2. Use code MSCUST for a $150 discount. Prices go up Feb 11th.
Register todayThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric
Returns a character string after truncating all trailing spaces.
Removes space character char(32)
or other specified characters from the end of a string.
Transact-SQL syntax conventions
Syntax for SQL Server prior to SQL Server 2022 (16.x):
RTRIM ( character_expression )
Syntax for SQL Server 2022 (16.x) and later, Azure SQL Managed Instance, Azure SQL Database, Azure Synapse Analytics, and Microsoft Fabric:
Important
You will need your database compatibility level set to 160 to use the optional characters argument.
RTRIM ( character_expression , [ characters ] )
An expression of character or binary data. character_expression can be a constant, variable, or column. character_expression must be of a data type, except text, ntext, and image, that is implicitly convertible to varchar. Otherwise, use CAST to explicitly convert character_expression.
Applies to: SQL Server 2022 (16.x) 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.
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
.
To enable the optional characters positional argument, enable database compatibility level 160
on the database(s) that you are connecting to when executing queries.
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's the result set.
Removes trailing spaces.
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's 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.
Important
You will need your database compatibility level set to 160 to use the optional characters argument.
The following example removes the characters abc.
from the end of the .123abc.
string.
SELECT RTRIM('.123abc.' , 'abc.');
Here's the result set.
.123
Events
Mar 31, 11 PM - Apr 2, 11 PM
The ultimate SQL, Power BI, Fabric, and AI community-led event. March 31 - April 2. Use code MSCUST for a $150 discount. Prices go up Feb 11th.
Register today