Solved

Using BCP Utility to Import NULL DateTime values

Posted on 2013-01-27
3
1,074 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
ID: 38825113
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 77

Expert Comment

by:arnold
ID: 38825364
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
ID: 38826302
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

912 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

24 Experts available now in Live!

Get 1:1 Help Now