The SQL RTRIM is one of the SQL String Function, which is used to remove the empty spaces from the Right hand side of a character expression. In this article we will show you, How to write / use RTRIM in SQL Server with example.
SQL RTRIM Function Syntax
The basic syntax of the SQL Server RTRIM Function is as shown below:
SELECT RTRIM (Character_Expression) FROM [Source]
Character_Expression: Please specify the valid Expression on which you want to remove tailing empty spaces (performing trimming on right side). RTRIM Function will remove the tailing 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.
TIP: You can use the SQL CAST function to explicitly convert the Character_Expression
This function will return VARCHAR or NVARCHAR
SQL RTRIM Function Example 1
The RTRIM Function is used to return 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 RTRIM function.
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;
Below lines of code is used to declare 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 ‘. Following statement will remove those extra spaces from the right side of a string variable. Here, We used the ALIAS Column to assign new name to the expression result as ‘Result’ Column.
SELECT RTRIM(@i) AS Result;
Next line, We used the RTRIM function directly on a string data
SELECT RTRIM('Welcome to T-SQL ') AS Result;
You can see from the below statement, statement had some leading spaces before and after the string. Following statement 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 RTRIM function on Column Names. Generally, we may get data with some extra spaces due to typing mistake, or any other data entry issues, and if you find such data then, you have to 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 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 RTRIM function along with CONCAT. For this, We are going to use the below shown data
Following 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]