Why the SQL Server ignore the empty space at the end automatically?

SQL Server is following the ANSI/ISO standard for string comparison.

The article How SQL Server Compares Strings with Trailing Spaces explains this in detail.

SQL Server follows the ANSI/ISO SQL-92 specification… on how to
compare strings with spaces. The ANSI standard requires padding for
the character strings used in comparisons so that their lengths match
before comparing them. The padding directly affects the semantics of
WHERE and HAVING clause predicates and other Transact-SQL string
comparisons. For example, Transact-SQL considers the strings ‘abc’ and
‘abc ‘ to be equivalent for most comparison operations.

Also, as explained in the article, if you compare with LIKE you do not get this behaviour.

Leave a Comment