Solved

Trailing Spaces = Trailing Nothings

Posted on 2008-10-21
7
365 Views
Last Modified: 2012-06-22
All -

I have a two rows of data in which one of the columns has the following data :

'1234567'
'1234567       ' (Seven trailing spaces)

When I run a query against the Column_name = '1234567', it returns both of the rows - SHould it?
When I run a query against the Column_name like '1234567 %', it returns the rows with trailing spaces - Which makes sense.

When I run a query against the LEN(Column_name) - it both returns '7' which I am assuming LEN function does not count the trailing spaces - please confirm.

0
Comment
Question by:up1973
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22767112
check this one:
SELECT DATALENGTH(Column_name) 

Open in new window

0
 
LVL 5

Expert Comment

by:jose_juan
ID: 22767520
Hi,

LEN function convert explicitly your parameter to nvarchar and to varchar datatypes conversion truncate final spaces. This is the reason.

Warning! if you use DATALENGTH this function return the bytes of the data expression, different

A trivial example

    SELECT datalength( N' ' ), datalength( ' ' )

return 2 and 1 length. But... What do you try compare?

Regards!

0
 

Author Comment

by:up1973
ID: 22767533
That help in understanding the data length is sql server - also is

'1234567' = '1234567       ' (seven spaces)?

I have a two rows of data in which one of the columns has the following data :

'1234567'
'1234567       ' (Seven trailing spaces)

When I run a query against the Column_name = '1234567', it returns both of the rows - SHould it?
When I run a query against the Column_name like '1234567 %', it returns the rows with trailing spaces - Which makes sense.

thanks  for your help.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 5

Expert Comment

by:jose_juan
ID: 22767694
Hi,

the best way for understand it is read documentation of your SQL version.

then, try examples

set nocount on

declare @a nvarchar(100)
declare @b varchar(100)

set @a = N'1234    '
set @b = '1234    '

select dlA = datalength( @a ), dlB = datalength( @b )
select lA = len( @a ), lB = len( @b )

if @a = @b
 print 'Equals'
else
 print 'Not equals'

Results

dlA         dlB        
----------- -----------
16          8

lA          lB          
----------- -----------
4           4

Equals

...

What do you try do?
@a must be equal or different to @b? (on your predicate of course)

Regards!
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 125 total points
ID: 22769028
>> Column_name = '1234567', it returns both of the rows - SHould it? <<

Yes.  Spaces is the standard padding character, so the two values can legitimately be considered equal.


>> LEN(Column_name) for both returns '7', [so] I am assuming LEN function does not count the trailing spaces <<

Correct.  For some reason, LEN() ignores trailing blanks.
0
 

Author Closing Comment

by:up1973
ID: 31508266
thank you - that answers my question
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22770845
I think, this should have been a point split with jose_juan, who explained the "why":
LEN function convert explicitly your parameter to nvarchar and to varchar datatypes conversion truncate final spaces. This is the reason.

Open in new window

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now