Skip to content

Latest commit

 

History

History
115 lines (85 loc) · 4.2 KB

File metadata and controls

115 lines (85 loc) · 4.2 KB

title: "RTRIM (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "07/05/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw" ms.reviewer: "" ms.suite: "sql" ms.technology: t-sql ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords:

  • "RTRIM_TSQL"
  • "RTRIM" dev_langs:
  • "TSQL" helpviewer_keywords:
  • "RTRIM function"
  • "character strings [SQL Server], trailing blanks"
  • "blank characters [SQL Server]"
  • "trailing blanks" ms.assetid: 52fd6e8d-650c-4f66-abcf-67765aa5aa83 caps.latest.revision: 40 author: MashaMSFT ms.author: mathoma manager: craigg monikerRange: ">=aps-pdw-2016||=azuresqldb-current||=azure-sqldw-latest||>=sql-server-2016||=sqlallproducts-allversions||>=sql-server-linux-2017"

RTRIM (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2008-all-md]

Returns a character string after truncating all trailing spaces.

Topic link icon Transact-SQL Syntax Conventions

Syntax

RTRIM ( character_expression )  

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.   ');  

[!INCLUDEssResult]

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.';  

[!INCLUDEssResult]

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  

[!INCLUDEssResult]

-------------------------------------------------------------------------  
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)

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)