Export to csv empty string as NULL

801 Views Asked by At

I'm trying to export some data from MsSQL to a CSV file using sqsh.

Assuming the SQL statement is SELECT * from [dbo].[searchengines].

The resulting CSV is something like these,

field1,field2,field3,field4,field5 
,,"Google",, 
,,"Yahoo",, 
,,"Altavista",, 
,,"Lycos",,

What can I do to make it into something like these :

field1,field2,field3,field4,field5 
NULL,NULL,"Google",NULL,NULL 
NULL,NULL,"Yahoo",NULL,NULL
NULL,NULL,"Altavista",NULL,NULL
NULL,NULL,"Lycos",NULL,NULL

I basically want to change fields that are empty into NULL. Any idea?

1

There are 1 best solutions below

0
Martin Wesdorp On

Unfortunately the empty string in csv output for nullable columns is hard coded in sqsh. See src/dsp_csv.c on line 144 where the call is made:

dsp_col( output, "", 0 );

You could replace it by

dsp_fputs( "NULL", output );

and rebuild sqsh. In the next release I will come up with a more elaborate solution.