Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to load null values into text fields using BCP

Status
Not open for further replies.

ddiamond

Programmer
Apr 22, 2005
918
US
I am using BCP to import a fixed width flat file into an sql server staging table. The problem is that when a text field is blank, BCP is loading spaces into the staging table. I would like BCP to treat a blank field as null, not spaces. Is this possible?

- Dan
 
Just a guess, but...

I suggest you try adding the -k command line parameter to your bcp command. -k is for "keep null values". More here:


I never used this, but I suspect it's worth a try.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I tried it, but it did not work. The problem is since it is a fixed width flat file, there is no way to distinguish a blank field from a field that is all spaces. I would like bcp alway treat all spaces as a null.
 
There may be a method that I am not aware of.

What you could do is to import the data in to a temporary holding table. Then write a query that inserts the data in to your real table. Something like...

Code:
Insert Into RealTable(Col1, Col2)
Select Col1, [!]NullIf([/!]Col2[!], ' ')[/!]
From   TemporaryHoldingTable

Generally, it's best to avoid temporary holding tables if possible, but it's also a good technique to use when you need to "clean up" your data prior to loading in to real tables.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
George,

Thanks for the tip. I may have to use a holding table like you suggested.

- Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top