Spaces in WHERE clause for SQL Server

  • A+
Category:Languages

I want to find out the records in which a certain column contains exactly one space and nothing else. So I wrote the first of the following queries:

select COUNT(*) from mytable where col = ' ' -- One space  select COUNT(*) from mytable where col = '  ' -- Two spaces  select COUNT(*) from mytable where col = '  ' -- Three spaces 

However, all three queries return the exact same records. Does Microsoft SQL Server not distinguish between the amount of spaces? How can I query exactly for one, two or more spaces?

 


Yes, it ignores trailing spaces in comparisons.

You can try to append a delimiting character.

SELECT count(*) FROM mytable WHERE col + 'X' = ' X'; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: