By: Daniel Calbimonte
The TRIM function is used to remove trailing and leading spaces (char(32)) from a string of characters. This was introduced with SQL Server 2017.
Syntax
TRIM(expression)
Parameters
- expression - the string to remove leading and trailing spaces.
Simple TRIM Example
The following example will TRIM the leading and trailing spaces.
SELECT TRIM(' What a wonderful world ') as msg
TRIM Example Using Table Column
The following example shows how to remove trailing and leading spaces from a table column.
SELECT TRIM(AddressLine1) as addressline FROM [Person].[Address]
Example with TRIM and CONCAT Functions
The following example shows how to use TRIM with CONCAT to trim leading and trailing spaces and then concatenate the values.
DECLARE @string1 varchar(30) = ' this is the first message ' DECLARE @string2 varchar(30) = ' this is the second message ' SELECT CONCAT(TRIM(@string1), ' ', TRIM(@string2)) as example
Related Articles
Last Update: 11/11/2021