SQL RTRIM Function

The SQL Server RTRIM function is a String Function that removes the empty spaces from the Right hand side of a character expression. The syntax of the SQL RTRIM Function is

SELECT RTRIM(Character_Expression)
FROM [Source]

Character_Expression: Please specify the Expression on which you want to remove tailing empty spaces (perform trimming on the right side). The SQL RTRIM Function removes the trailing blanks from the right side of this expression. This Character_Expression can be a variable, constant, or column of any data type, except image, text, and NText.

Return Type: It will return VARCHAR or NVARCHAR

SQL RTRIM Function Example

The SQL RTRIM Function returns the character expression after removing the empty spaces from the right side of the given expression. The following query will show multiple ways to use this function.

TIP: You can use the CAST to convert the Character_Expression in SQL Server explicitly

DECLARE @i VARCHAR(50)
SET @i = 'Tutorial Gateway      '

SELECT RTRIM(@i) AS Result;

SELECT RTRIM('Welcome to T-SQL           ') AS Result;  

SELECT RTRIM('      SQL Server Tutorials at Tutorial Gateway        ') AS Result;
RTRIM Example 1

The below code is used to declare the VARCHAR variable and assign the string data.

DECLARE @i VARCHAR(50)
SET @i = 'Tutorial Gateway      '

The above statement shows that it had some tailing spaces after the string ‘Tutorial Gateway ‘. The following statement removes those extra spaces from the right side of a string variable.

SELECT RTRIM(@i) AS Result;

Next line, We used the SQL RTRIM function directly on a string of data.

SELECT RTRIM('Welcome to T-SQL           ') AS Result;

You can see this from the below statement. It had some leading spaces before and after the string. The below line will remove the extra spaces from only the right side of the specified expression.

SELECT RTRIM('      SQL Server Tutorials at Tutorial Gateway        ') AS Result;

SQL RTRIM Function Example 2

This example will show you how we will use the RTRIM function on Column Names. Generally, we may get data with some extra spaces due to typing mistakes or any other data entry issues. If you find such data, use this RTRIM or LTRIM to trim those extra spaces.

SELECT TOP 10 [ProductKey]
      ,LTRIM([EnglishProductName]) AS [ProductName]
      ,[Color]
      ,[ModelName]
      ,LTRIM([EnglishDescription]) AS [Description]
  FROM [AdventureWorksDW2014].[dbo].[DimProduct]
  WHERE [EnglishDescription] IS NOT NULL
SQL RTRIM Function 3

RTRIM Example 3

In general, you should be very careful with extra spaces while you are concatenating strings. In this example, We are going to use the SQL RTRIM function along with CONCAT. For this String method, we use the below-shown data

String Table 3

The following RTRIM statement will remove white spaces from the right-hand side of the [FirstName] and [LastName] columns.

SELECT RTRIM([FirstName]) + ' ' + RTRIM([LastName]) AS [FullName]
      ,[Education]
      ,[Occupation]
      ,[YearlyIncome]
      ,[Sales]
      ,[HireDate]
  FROM [Employee]
SQL RTRIM Function 2
Categories SQL