Solved

Using BCP Utility to Import NULL DateTime values

Posted on 2013-01-27
3
1,046 Views
Last Modified: 2013-01-28
I'm setting up a simple system whereby job data is exported from one database and imported into another.

The export is done by way of a stored procedure, which returns output, mostly from a view and then a bcp statement creates a csv file. The problem I have is that I need one of the smalldatetime fields to be null so I'm outputting '' in the stored procedure. The csv file created however, contains a character between the comma delimiters (maybe nul byte, not sure). This gives an error on import.

If I manually edit the csv file to contain nothing between the commas, it imports fine so how can I output a NULL correctly in the first place? I see this question asked several times on various sites but I've yet to find a definitive answer I can easily implement.

The bcp output statement is as follows.
bcp "execute usp_export_jobs" queryout %filename%_job.csv -U %username% -P %password% -S .\SQLEXPRESS -d %dbname% -c -t"," -r"\n"

Open in new window


The stored procedure select statement is the equivalent of
SELECT '' AS target_date

Open in new window


I thought I'd solved this last week by adding the -k option to the import statement but looks like a temporary fluke.

The bcp import statement to table [jobs] is as follows (I create a fmt file in code just before this)
bcp jobs in %filename% -U %username% -P %password% -S .\SQLEXPRESS -d %dbname2% -e %filename%_errors.txt -f %filename%.fmt -k

Open in new window


Thanks
0
Comment
Question by:nigelr99
3 Comments
 
LVL 42

Accepted Solution

by:
EugeneZ earned 500 total points
Comment Utility
if I understood your quest right:
try to ise in the proc

SELECT NULL AS target_date
or
SELECT ' ' AS target_date

instead of :

SELECT '' AS target_date
0
 
LVL 76

Expert Comment

by:arnold
Comment Utility
You can redefine your import map to check whether the parameter is a space or null and set it if needed based on the column definition.

Null might also cause an error if not null is set on the column.
0
 

Author Closing Comment

by:nigelr99
Comment Utility
As simple as that!

SELECT NULL AS target_date worked a treat (embarrassingly!)

I could swear I tried that when I had the problem last week but maybe distracted by another issue somewhere else as well.
Many thanks!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

763 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

10 Experts available now in Live!

Get 1:1 Help Now