SQL Server RTRIM() function is used to trim trailing characters from a character string.
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.
Recommended for you