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"
The stored procedure select statement is the equivalent of
SELECT '' AS target_date
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