Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

string Comparison. Expand / Collapse
Author
Message
Posted Monday, March 14, 2011 2:52 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, August 25, 2014 12:47 AM
Points: 50, Visits: 118
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
Post #1077595
Posted Monday, March 14, 2011 3:46 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 2:39 AM
Points: 1,880, Visits: 2,846
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.
Post #1077606
Posted Monday, March 14, 2011 3:52 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, August 25, 2014 12:47 AM
Points: 50, Visits: 118
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.


Post #1077611
Posted Monday, March 14, 2011 3:57 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 40,385, Visits: 36,827
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

Post #1077613
Posted Monday, March 14, 2011 3:58 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 10:25 AM
Points: 40,385, Visits: 36,827
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

Post #1077614
Posted Monday, March 14, 2011 4:00 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 2:39 AM
Points: 1,880, Visits: 2,846
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.
Post #1077615
Posted Monday, March 14, 2011 8:17 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, August 25, 2014 12:47 AM
Points: 50, Visits: 118
Thanks you Gail.

Now this makes sense.
I didn't realize this behaviour till now.
Thanks for enlighten me.
Post #1077729
Posted Thursday, June 16, 2011 2:50 PM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Yesterday @ 12:05 PM
Points: 271, Visits: 1,021
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
Post #1126964
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse