dbTalk Databases Forums  

bcp is inserting blank space for empty string

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss bcp is inserting blank space for empty string in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
epaetz
 
Posts: n/a

Default bcp is inserting blank space for empty string - 04-04-2005 , 04:19 PM






I'm doing a bcp out of a table to a file. Some of the fields in a
record may have an empty string.

When I bcp out to the file and examine it, the fields that have an
empty string in the database now show up in the file as having one
blank character.

Why is bcp doing this? I don't want the blank character in my output.

Thanks,
Eric


Reply With Quote
  #2  
Old   
Simon Hayes
 
Posts: n/a

Default Re: bcp is inserting blank space for empty string - 04-05-2005 , 01:47 AM






Which version of MSSQL? What data type is the column? Is ANSI_PADDING
on or off for the column? If your column is char(1), then this would be
expected, as char columns are padded out with spaces; a varchar should
not be padded, though.

If this doesn't help, I suggest you post (simplified) CREATE TABLE and
INSERT statements to show exactly what your table and data look like.

Simon


Reply With Quote
  #3  
Old   
epaetz
 
Posts: n/a

Default Re: bcp is inserting blank space for empty string - 04-05-2005 , 07:40 AM




Simon Hayes wrote:
Quote:
Which version of MSSQL? What data type is the column? Is ANSI_PADDING
on or off for the column? If your column is char(1), then this would
be
expected, as char columns are padded out with spaces; a varchar
should
not be padded, though.

If this doesn't help, I suggest you post (simplified) CREATE TABLE
and
INSERT statements to show exactly what your table and data look like.

Simon
SQL Server 2000
Varchar 16
ANSI_PADDING is off.

Give it a shot. Create a table with three columns, all varchar. Insert
an empty string into each to get rid of any nulls. Then do a bcp out to
an output file and let me know if you get the same results.

Eric



Reply With Quote
  #4  
Old   
Simon Hayes
 
Posts: n/a

Default Re: bcp is inserting blank space for empty string - 04-06-2005 , 03:20 PM




"epaetz" <epaetz41 (AT) hotmail (DOT) com> wrote

Quote:
Simon Hayes wrote:
Which version of MSSQL? What data type is the column? Is ANSI_PADDING
on or off for the column? If your column is char(1), then this would
be
expected, as char columns are padded out with spaces; a varchar
should
not be padded, though.

If this doesn't help, I suggest you post (simplified) CREATE TABLE
and
INSERT statements to show exactly what your table and data look like.

Simon

SQL Server 2000
Varchar 16
ANSI_PADDING is off.

Give it a shot. Create a table with three columns, all varchar. Insert
an empty string into each to get rid of any nulls. Then do a bcp out to
an output file and let me know if you get the same results.

Eric

I used this test script (on 8.00.760 Enterprise):

set ansi_padding off
go
create table eric (col1 varchar(16) null)
go
insert into eric select '' -- empty string
insert into eric select ' ' -- single space
go
select col1, len(col1) as 'Length', datalength(col1) as 'Datalength', col1 +
'X'
from eric
go

Then I exported the file with bcp:

bcp Development..eric out c:\temp\eric.txt -S kilkenny -c -T

When I checked eric.txt with a hex editor, it showed this:

00 0D 0A 20 0D 0A

So the empty string is an ASCII NUL character in this case, but the space is
ASCII 20. Is this the behaviour you see, or do you get something different?
Setting ANSI_PADDING ON didn't change the output (and BOL recommends it
should always be on anyway).

Simon




Reply With Quote
  #5  
Old   
epaetz
 
Posts: n/a

Default Re: bcp is inserting blank space for empty string - 04-08-2005 , 03:32 PM



I found a work around, using a Case structure in the sql query to
change empty string to a null. The BCP in turn outputs the resulting
null as an empty!

Thanks for your assistance.

Eric


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.