Scenario:
I have some requirements: 1. Save the SQL result as CSV file 2. Import the data from the CSV file into another SQL database. There was not a way to copy the data from one sql to another sql. So I was saving the SQL result as CSV file it was saving NULL values as NULL string the CSV file. As a result of this when I was importing the CSV file into SQL table it had NULL string rather than NULL value.
SQL:
CSV:
Solution:
There is no way you can enter null values in excel. So best bet is:
- Save the SQL result as CSV file
- Open the CSV file in notepad++ and replace all the NULL with empty string (not a space)
- Replace a column delimeter to use comma rather than tab if you need
- Save the file as xlsx. If you import excel file to sql table then it does put blank as NULL, it you import csv, it does put blank as blank string
- SSIS data flow task: Destination editor–>Check option – keep nulls
- Now it will the keep the null as null
Alternative:
- Load the flat file with NULL string in the staging table
- Add a SQL task to update the staging table with NULL where NULL string