Skip to content
Home ยป SQL Server RTRIM()

SQL Server RTRIM()

SQL Server RTRIM() function is used to trim trailing spaces from a character string.




SYNTAX

RTRIM(inputstring)

Lets look at an example of RTRIM() function in SQL Server.

DECLARE @name VARCHAR(50) = 'MR John watlerย  ย '

SELECT @name AS NAME, DATALENGTH(@name) AS NAMELENGTH

You can see above result, the DataLenght function returns a length of string that is 17 which is after including three trailing spaces but in actual the length of name string is 14.

So, to get the actual length of name string after removing those trailing spaces from string, you can use RTRIM() function.

Following SQL statement uses RTRIM() function to remove the trailing spaces from string.

DECLARE @name VARCHAR(50) = 'MR John watlerย  ย '

SELECT @name AS NAME , DATALENGTH(@name) AS DATALENGTH , 
DATALENGTH(RTRIM(@name)) AS DATALENGTH_USING_RTRIM 

Now you can see, all the trailing spaces from string are removed and the length ofย  string is now 14.

Also Read..

LTRIM()

DATALENGTH()

TRIM()




Loading

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.