SQL Server RTRIM Function - Features, Examples and Equivalents

RTRIM function truncates all trailing blanks from a string.

Syntax RTRIM(string)
Quick Example SELECT RTRIM('A ');
CHAR(160) Non-breaking space (Unicode 160, or HTML  ) is not removed by RTRIM

Versions: SQL Server 2008 and 2005

Related Functionality in SQL Server

Related functions for RTRIM in SQL Server:

LTRIM(string) Removes leading blanks
SUBSTRING(string, start_pos, length) Returns a substring
CAST(string AS CHAR(n) Truncates string to n characaters
CONVERT(CHAR(n), string) Truncates string to n characaters
REPLACE(string, what, with) Replaces a substring or character in string

SQL Server RTRIM Function Details

RTRIM function truncates trailing spaces from a string expression:

  SELECT '|' + RTRIM('AB   ') + '|';
  -- Result: |AB|

Non-breaking Space CHAR(160) is not Removed

CHAR(160) (or   widely used in HTML) that can appear in VARCHAR and NVARCHAR is not removed by RTRIM:

  CREATE TABLE t_rtrim1
  (
     c1 VARCHAR(3),
     c2 NVARCHAR(3)
  );
 
  INSERT INTO t_rtrim1 VALUES ('A ', 'A ');
  INSERT INTO t_rtrim1 VALUES ('A' + CHAR(160), 'A' + CHAR(160));
 
  SELECT '''' + c1 + '''', '''' + RTRIM(c1) + '''' FROM t_rtrim1;
 
  SELECT '''' + c2 + '''', '''' + RTRIM(c2) + '''' FROM t_rtrim1;

Both queries return:

Column Value Value after RTRIM
Regular Space 'A ' 'A'
Non-breaking space 'A ' 'A '

Resources