Solved

Trailing Spaces = Trailing Nothings

Posted on 2008-10-21
7
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 143

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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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:
Scott Pletcher 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 143

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how the fundamental information of how to create a table.

707 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