?
Solved

Using BCP Utility to Import NULL DateTime values

Posted on 2013-01-27
3
Medium Priority
?
1,223 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 43

Accepted Solution

by:
Eugene Z earned 2000 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 79

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

741 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