SQL RTRIM Function

The SQL RTRIM function is a SQL String Function that removes the empty spaces from the Right hand side of a character expression. The syntax of the SQL Server 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 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: This function will return VARCHAR or NVARCHAR

SQL RTRIM Function Example 1

The 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 the RTRIM function.

TIP: You can use the CAST function 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;
SQL RTRIM Function 1

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

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

From the above statement, you can see 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 data

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

You can see 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 are going to use the SQL RTRIM function on Column Names. Generally, we may get data with some extra spaces due to typing mistake, 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 Function Example 3

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

SQL RTRIM Function 3

The following SQL rtrim statement will remove white spaces from right hand side of the [FirstName] column and [LastName] column.

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