Overview
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.
Explanation
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
Additional Information

Daniel Calbimonte is a Microsoft Most Valuable Professional, Microsoft Certified Trainer and Microsoft Certified IT Professional for SQL Server. He is an accomplished SSIS author, teacher at IT Academies and has over 10 years of experience as a QE and developer for SQL Server related software. He has worked for the government, oil companies, web sites, magazines and universities around the world. Daniel also regularly speaks at SQL Servers conferences and blogs.
- MSSQLTips Awards: Author of the Year Contender – 2015-2018, 2022, 2023 | Champion (100+ tips) – 2018



Hi Shair, we will update these based on your recommendation.
Thanks
Greg
It would be nice to have the SQL version the function works in. I have SQL 2016 and saw *TRIM* – wow, I had missed it … but no, it works form 2017 forward. sigh. Thanks for the post though! Great information.