Difference between LTRIM and LTRIM with RTRIM [closed]

This is not the question the OP asks in their question, but in the comments, but sounds like it’s their real question. I am therefore putting this in as an answer for posterity:

I am get len(LTRIM(RTRIM(' Removes trailing spaces. '))) and len(LTRIM(' Removes trailing spaces. ') . is equals.

When determining equality SQL Server ignores trailing spaces. 'Trailing Spaces' = 'Trailing Spaces ' would return True.

This is not the case for leading spaces. ' Leading Space' = 'Leading Space' would not return True.

Thus, when comparing 2 values, if they might have varying numbers of leading spaces, you would need to trim (LTRIM) those, but for comparison purposes (only) you would not need to trim (RTRIM) the trailing ones.

Of course, you could trim both with TRIM in more recent versions of SQL Server (2017+), however, any trim operations will make your query non-SARGable, which could (significantly) effect performance. If you need to store untrimmed and trimmed data, you are better off with the latter being a PERSISTED computed column you can index.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top