M HYPE SPLASH
// updates

How to write "NULL" into CSV from Excel for blank fields

By Emily Wilson

How do I write "NULL" for blank fields when I export a CSV file from Excel 2007? Is there a feature to do that?

1

5 Answers

While exporting, i think it may not be possible.

But you can also try this way before saving or after saving

1. Click F5
2. Click Special
3. Select Blanks
4. Click OK
5. Type NULL
6. Press CTRL + Enter

OR

With macro, VB code:

Sheet1.UsedRange.SpecialCells(xlCellTypeBlanks)="NULL" 
1

Open the CSV file in a text editor, such as Notepad, and do a find/replace all ,, => ,NULL,. It's not within Excel, but it will still work.

1

I know this works in newer Excel; not sure about the older version.

  1. Select Cell A1; then shift click the most bottom right selection
  2. Press Ctrl+H
  3. In the "Find" field leave it blank
  4. in the "Replace With" field put in NULL

This should replace all the nothings with 'NULL' in the selected cells.

Short answer: You don't do anything.

There is no difference between an Excel cell with an empty string or one with no value (null) for purposes of exporting a CSV. The fields in CSV files don't actually have data types. So you will get an empty field in your CSV file either way. Example:

enter image description here

Will generate a CSV that contains:

12,,54

Nothing between the two delimiters (commas) means empty or null. How the empty value is dealt with depends on the software that is reading the CSV file.

2

I don't know Excel but this python script will do it

import csv,sys
fin = open(sys.argv[1],'rb')
fout = open(sys.argv[2],'wb')
reader = csv.reader(fin, delimiter=',', quotechar='"')
writer = csv.writer(fout, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for row in reader: new = [] for s in row: if s=='': new.append('null') else: new.append(s) writer.writerow(new)
fin.close()
fout.close()
4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy