SQL Server RTRIM Function
The RTRIM function is used to remove trailing spaces from a string of characters or character expressions. R is for right and TRIM means to remove unwanted spaces.
- expression - this is the string or expression with trailing spaces that you want to right trim.
Simple RTRIM Example
The following example will RTRIM the trailing spaces after the word "world" only.
SELECT RTRIM(' What a wonderful world ')
RTRIM Example with Table Column
The following example shows how to remove trailing spaces from a table column.
SELECT RTRIM(AddressLine1) as addressline FROM [Person].[Address]
Example with RTRIM and CONCAT
The following example shows what happens if we use RTRIM and CONCAT to concatenate strings.
- The first column "example1", the CONCAT id done first and then the RTRIM.
- The second column "example2", the RTRIM is done first and then the CONCAT.
DECLARE @string1 varchar(30) = 'this is the first message ' DECLARE @string2 varchar(30) = 'this is the second message ' SELECT RTRIM(CONCAT(@string1, @string2)) example1, CONCAT(RTRIM(@string1), RTRIM(@string2)) example2
Example1 shows the trailing spaces for string1 remain, but the trailing spaces for string2 are removed.
Example2 shows the trailing spaces for both string1 and string2 have been removed.