Looks like SQL Server (tried on 2008 R2) is doing an RTRIM on columns in GROUP BY clause. Did anyone notice this? Am I missing something here?
The two selects are returning the same result set in the query below, which should not be the case I believe.
declare @t table(Name varchar(100), Age int)
insert into @t values ('A', 20)
insert into @t values ('B', 30)
insert into @t values ('C', 40)
insert into @t values ('D', 25)
insert into @t values (' A', 21)
insert into @t values ('A ', 32)
insert into @t values (' A ', 28)
select
Name,
count(*) Count
from @t
group by Name
select
rtrim(Name) RtrimmedName,
count(*) Count
from @t
group by rtrim(Name)
Please let me know your thoughts…
It’s actually doing the opposite, but the observable effects are the same.
When comparing two strings of unequal length, one of the rules of SQL (the standard, not just SQL Server) is that the shorter string is padded with spaces until it’s the same length, and then the comparison is performed.
If you want to avoid being surprised, you’ll need to add a non-space character at the end of each string.
In fact, checking the standard text, it appears that there are two options:
But all of the SQL Server collations I’m aware of are
PAD SPACE.