But in this example, SQL server isn't comparing strings at all. It's comparing integer values (the length of each string.)
The correct explanation is that SQL server ignores trailing spaces when calculating the length of a string.
Not exactly so. SQL server ignores trailing spaces only when calculating the length of a string using the len function
, which is not the only one to calculate the lenght of a string. If you need to account for the trailing spaces you can use the datalength function instead as the latter does not ignore the trailing spaces. For example,
select len('hello ') len_function, datalength('hello ') datalength_function
will yield the following result:
Since the QoD specified len, the one ignoring the spaces, the answer is 10. If the datalength was used insead, the answer would be 0.
Very nice question, really liked it!