Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2008: How to find trailing spaces

How can I find all column values in a column which have trailing spaces? For leading spaces it would simply be

select col from table where substring(col,1,1) = ' '; 
like image 705
atricapilla Avatar asked Mar 31 '10 11:03

atricapilla


People also ask

How do I find trailing spaces in SQL Server?

SQL LEN() SQL LEN() function is used to count the number or length of character in given string. It returns integer value always and null for empty string.

What are trailing spaces in SQL?

When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs.


2 Answers

You can find trailing spaces with LIKE:

SELECT col FROM tbl WHERE col LIKE '% ' 
like image 53
Jaxidian Avatar answered Sep 29 '22 19:09

Jaxidian


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 WithTrail 0       1   
like image 38
Neil Moss Avatar answered Sep 29 '22 21:09

Neil Moss