Solved

Trailing Spaces = Trailing Nothings

Posted on 2008-10-21
7
363 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

759 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

19 Experts available now in Live!

Get 1:1 Help Now