Tutorialsteacher

Follow Us

SQL Server RTRIM() Function: Removes Trailing Spaces

In SQL Server, the RTRIM() function removes all the trailing spaces from right side in the specified string and returns a new string.

RTRIM(input_string)

Parameters

input_string: A string that can be a constant, variable, or a table column. It must be of a data type that can be implicitly convertible to varchar data type else use CAST to explicitly convert.

Return Value

Returns a string without trailing space of varchar or nvarchar type.

Example 1:

In the following example, the RTRIM() is used to trim the trailing spaces in a string, as shown below.

Example: RTRIM()
SELECT RTRIM('Have a nice day!         ')

Example 2:

In the following example, the input string has leading and trailing blanks. the RTRIM() function truncates the trailing blanks and the blanks at the beginning of the string are not truncated.

Example: RTRIM()
SELECT RTRIM('    Have a nice day!         ') AS Greetings;

Use the LTRIM() and RTRIM() functions together to truncate leading and trailing blanks from the string, as shown below.

Example: RTRIM()
SELECT LTRIM(RTRIM('   Have a nice day!         ')) AS Greetings;

Example 3:

The following example shows a string having trailing blanks joined with another string. The result shows the RTRIM() function removes the trailing spaces from the first string.

Example: RTRIM() RTRIM()
SELECT RTRIM('Have a nice day!       ') + ' Goodbye' AS Greetings;

Example 4:

In the following SQL statement, the RTRIM() function is used to trim all trailing blanks from the Email column of the Employee table.

Example: RTRIM()
Update Employee
SET EMail = RTRIM(EMail)