SQL TRIM Function

TRIM removes leading and trailing spaces from a string.

TRIM can also remove other characters with an optional parameter.

This function can be replaced by combining LTRIM and RTRIM functions.

Example

#

This example removes leading and trailing spaces from the input string.

SELECT TRIM('  SQL  ') AS Trimmed
Result:  1 record
Trimmed
SQL

Syntax

Syntax for the TRIM function.

TRIM ([characters FROM] string)

[characters FROM ] is optional and specifies characters to be removed.

[characters FROM ] is only supported in SQL Server 2017 and newer.

string -- a string or column name to trim.


More Examples

TRIM. Other characters.

Problem: Remove the specified start and end characters (!, #, and space) from the string.
SELECT TRIM('#! ' FROM ' #! #SQL !    ') AS Trimmed

Supported in SQL Server 2017 and up.

Result:  1 record
Trimmed
SQL

You may also like



Guides


vsn 3.1