2

Possible Duplicate:
SQL WHERE clause matching values with trailing spaces

When querying SQL Server (through Management Studio Express for what its worth), I've noticed that when querying a varchar field it doesn't matter if you have some trailing whitespace in your criteria, it will still match.

The below matches matched (so it ignores the trailing whitespace in the search criteria)

select top 10 * from sometable
where somecolumn = 'matched    '

I've tried this on 2005 and 2012, same result.

Any ideas why this happens? Is it something to do with how varchar are stored?

Community
  • 1
  • 1
Mr Grok
  • 3,876
  • 5
  • 31
  • 40
  • 1
    http://stackoverflow.com/a/4166186/284240 – Tim Schmelter May 23 '12 at 07:59
  • Does look to be but the tagging of that question ("C#, Database"!) means it didn't show up in title searches when writing the question. Suggest re-tagging of the old question. If someone that has permissions can do that please. After that then this can be closed as a duplicate. – Mr Grok May 23 '12 at 08:22

1 Answers1

4

It is a normal behaviour : http://support.microsoft.com/kb/316626/

'like' doesn't follow that behaviour :

select top 10 * from sometable
where somecolumn like 'matched    '

will not return "matched"