Solved

Trailing Spaces = Trailing Nothings

Posted on 2008-10-21
7
373 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 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

809 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