Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Forums
Practice Test
Bookstore
Tip of the Day : Last Day of the Month Function (EOMONTH)
SQL Server Helper
Home > > Tip of the Day
SQL Server Helper - Tip of the Day

The RTRIM string function returns a character expression after truncating all trailing blanks or spaces.   The syntax of the RTRIM string function is as follows:

RTRIM ( < character_expression > )

The < character_expression > is an expression of character or binary data and can be a constant, variable or column.  The < character_expression > must be of data type, except TEXT, NTEXT and IMAGE, that is implicitly convertible to VARCHAR.  Otherwise, the CAST function must be used to explicitly convert the < character_expression >

Usage #1 : Determine if a String Has Trailing Spaces

DECLARE @String      VARCHAR(100)
SET @String = 'String With Trailing Spaces       '

SELECT CASE WHEN DATALENGTH(@String) = DATALENGTH(RTRIM(@String))
       THEN 'No Trailing Spaces'
       ELSE 'With Trailing Spaces' END AS [Output]
Output
-----------
With Trailing Spaces

Usage #2 : Remove or Trim Leading Spaces Without Using LTRIM

DECLARE @String      VARCHAR(100)
SET @String = '     String with Leading Spaces'

SELECT REVERSE(RTRIM(REVERSE(@String))) AS [Output]
Output
-----------
String with Leading Spaces

Usage #3 : Imitate Oracle's TRIM Function

DECLARE @String      VARCHAR(100)
SET @String = '     SQL Server Helper     '
SELECT LTRIM(RTRIM(@String)) AS [Output]
Output
-----------
SQL Server Helper

Back to Tip of the Day List Next Tip