|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 11:17 PM
Points: 43,
Visits: 103
|
|
Maybe this is a widely known topic, but not for me. I found some strange behavior.
A table with Customer names, where in some cases trailing blanks where inserted. I wanted to know which names had trailing blanks. (Coumn type = NVARCHAR) so....
SELECT * FROM dbo.Customers WHERE RTRIM(LTRIM(name)) <> name No matches was the result. Huh? I know there are some. So i tried the same with LIKE operater. There you go! 47 records in my case.
Then i tried
SELECT * FROM dbo.Customers WHERE RTRIM(LTRIM(name))+'*' <> name+'*' Hm. There are my 47 records.
Since when is this true?
SELECT 1 WHERE 'test' = 'test ' Is this a Database setting or what? Any Explanations on this? Thanks for enlighten me
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:24 AM
Points: 1,871,
Visits: 2,692
|
|
try the following:
select LEN(string2),len(RTRIM(LTRIM(string2))) from yourtablename and compare the length it records.
---------------------------------------------- Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 11:17 PM
Points: 43,
Visits: 103
|
|
Thanks for the info.
I allready know there are different ways to solve this. that was not my problem
I was curious if anyone knew this behaviour, and why it works this way. I could'nt find this in BOL.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 37,741,
Visits: 30,019
|
|
JohnyRotten (3/14/2011)
Since when is this true? SELECT 1 WHERE 'test' = 'test '
In SQL always.
SQL ignores trailing spaces when doing string comparisons. If it didn't, there would be all sorts of fun in comparing a char(4) to a char(10) (seeing as char is space-padded to its full length)
If you want to compare lengths of strings, use DATALENGTH (not LEN, as that too ignores trailing spaces). In general though don't worry too much about trailing spaces, seeing as SQL ignores them for comparison, filters and joins. Unless you're also working with something that doesn't (like SSIS)
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 37,741,
Visits: 30,019
|
|
Henrico Bekker (3/14/2011)
try the following: select LEN(string2),len(RTRIM(LTRIM(string2))) from yourtablename and compare the length it records.
However note (from Books Online)
LEN (Transact-SQL)
Returns the number of characters of the specified string expression, excluding trailing blanks.
Gail Shaw Microsoft Certified Master: SQL Server 2008, MVP SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
We walk in the dark places no others will enter We stand on the bridge and no one may pass
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 2:24 AM
Points: 1,871,
Visits: 2,692
|
|
Ahh, excellent thanx Gail, I learnt something as well from this question. Great stuff!
---------------------------------------------- Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Monday, April 15, 2013 11:17 PM
Points: 43,
Visits: 103
|
|
Thanks you Gail.
Now this makes sense. I didn't realize this behaviour till now. Thanks for enlighten me. 
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Saturday, March 30, 2013 9:39 AM
Points: 261,
Visits: 966
|
|
Hi if you want to search in your table where the name filed has a leading or trailing space you could use the following
select * from table where patindex('% %',collumn)>0
if there is a space anywhere in the collum a result will be returned
*************************************************************
The first is always the hardest
|
|
|
|