SQL Server RTRIM Function


By:

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.

Syntax

RTRIM(expression)

Parameters

  • 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 T-SQL simple example

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]
Apply RTRIM function to columns

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.

concat and rtrim

Related Articles


Last Update: 11/10/2021




Comments For This Article





download














get free sql tips
agree to terms