SQL Server 2008: How to find trailing spaces SQL Server 2008: How to find trailing spaces sql-server sql-server

SQL Server 2008: How to find trailing spaces


You can find trailing spaces with LIKE:

SELECT col FROM tbl WHERE col LIKE '% '


SQL Server 2005:

select col from tbl where right(col, 1) = ' '

As a demo:

select     case when right('said Fred', 1) = ' ' then 1 else 0 end as NoTrail,    case when right('said Fred ', 1) = ' ' then 1 else 0 end as WithTrail

returns

NoTrail WithTrail0       1  


This is what worked for me:

select * from table_name where column_name not like RTRIM(column_name)

This will give you all the records that have trailing spaces.

If you want to get the records that have either leading or trailing spaces then you could use this:

select * from table_name where column_name not like LTRIM(RTRIM(column_name))