SQL Server RTRIM() Function

The RTRIM() function in SQL Server removes trailing spaces (spaces at the end) from a string. This is a very useful function for data cleaning and preparing strings for accurate comparisons or other operations.



RTRIM(): Definition and Usage

Extra whitespace at the end of a string can sometimes cause unexpected behavior in string comparisons or other text-based operations. RTRIM() efficiently removes only those trailing spaces, leaving the main string content intact. Leading spaces (spaces at the beginning of the string) or spaces within the string are not affected.

Syntax

Syntax

RTRIM(string)
      

Parameter Values

Parameter Description
string The string from which you want to remove trailing spaces. This is required.

Related Function

For removing leading spaces (spaces at the beginning of a string), use the LTRIM() function.

Example

Removing Trailing Spaces

This example removes trailing spaces from the string "SQL Tutorial ".

Syntax

SELECT RTRIM('SQL Tutorial    ') AS RightTrimmedString;
      
Output

SQL Tutorial