TRIM()–New String function in SQL Server 2017

SQL Server 2017 introduces long awaited string function which supports both LTRIM() and RTRIM() together

Syntax for the function is as follows

TRIM ( [ characters FROM ] string )

Example usage

SELECT TRIM(‘ SQLXpertise ‘) [SQL2017]

Output

In addition to trimming spaces, TRIM() function supports removing other characters in prefix and suffix as well, see below example, we are providing #, ~, <SPACE> as characters to removed from Prefix and Suffix

Example with Character removal

SELECT TRIM(‘#~ ‘ FROM ‘# SQLXpertise ~’) [SQL2017]

Output

This new function will be helpful in using one TRIM() call rather than 3 function calls of LTRIM(), RTRIM() and REPLACE()