SQL Server : Removes Leading and Trailing Spaces from a String

TRIM() Function

The TRIM() function is used to remove spaces at the leading (beginning) and/or trailing (end) of string text.

Syntax:

TRIM(text)

e.g.

DECLARE @String VARCHAR(50) = '   Learn to be happy right now!   '

SELECT TRIM(@String) TrimmedText


Although trim is usually used to remove spaces by default, we can also use TRIM to remove specific characters.

Syntax

TRIM([characters FROM ]text)

text is the source string text.
characters are characters to remove

e.g.

DECLARE @String VARCHAR(50) = '@!Learn to be happy right now!@!'

SELECT TRIM('@!' FROM @String) TrimmedText


Also read the following posts:

Removes Leading and Trailing Spaces from a String (TRIM)

Removes Leading Spaces from a String (LTRIM)

Removes Trailing Spaces from a String (RTRIM)

Post a Comment

0 Comments