Solved

Using BCP Utility to Import NULL DateTime values

Posted on 2013-01-27
3
1,137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 42

Accepted Solution

by:
Eugene Z 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 78

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how the fundamental information of how to create a table.

733 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