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
Solved

Using BCP Utility to Import NULL DateTime values

Posted on 2013-01-27
3
1,118 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

856 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