Calculate the NULL value using a Field Calculator

23818
6
09-11-2012 12:34 PM
JoannaLaroussi
New Contributor
I have a field type string with the values of ???A???, ???B??? and ???C??? and NULL (empty string). I would like to replace selected letter values with NULL, which will be an empty string, not a string ???NULL???. I tried to use in a field calculator a Python parser:
str!field_name!
but this did not work. I found plenty of posts how to select a NULL value, but nothing helpful on how to calculate NULL in a way that it is visible as NULL/empty string also through SQL server connection. Any ideas?
Tags (2)
0 Kudos
6 Replies
KevinBell
Occasional Contributor III
When using python, use "None" instead of Null.

PRELOGIC SCRIPT CODE:
def testIt(field):
    if field in ['A', 'B']:
        field = None
    return field

TYPE=

testIt(!yourfield!)
JoannaLaroussi
New Contributor
Thank you for your answer. I run it and nothing happened. Anyway, I do not want to replace all my �??A�?� or �??B�?�, but I am looking for a way to calculate NULL /None value for only selected rows.
0 Kudos
KevinBell
Occasional Contributor III
then don't use the codeblock and just use None which is null to python.
0 Kudos
JoannaLaroussi
New Contributor
I already tried to do this. I received an error:
ERROR 000539: error running expression: NONE <type'exceptions.NameError'> name 'NONE' is not defined.
If I use None (not a NONE like above) there is no error, but also nothing happen.
0 Kudos
ChrisSnyder
Regular Contributor III
Try this:

arcpy.MakeFeatureLayer(fc, "lyr", "MY_FIELD in ('A','B')":
if int(arcpy.GetCount_management("fl").getOutput(0)) > 0:
    arcpy.CalculateField_managment("fl", "MY_FIELD", "None", "PYTHON")
or
arcpy.MakeFeatureLayer(fc, "lyr", "MY_FIELD in ('A','B')":
if int(arcpy.GetCount_management("fl").getOutput(0)) > 0:
    arcpy.CalculateField_managment("fl", "MY_FIELD", "NULL", "VB")
0 Kudos
JoannaLaroussi
New Contributor
Thank you for your solutions. I had a problem with making it work: both codes returned an invalid syntax error and highlighted �??:�?� in a code. I did not do more research on this error, because I got another solution.

I just learned that in ArcGIS 10 Null can be simply calculated in a field calculator by typing Null (VB parser) in a calculator window.

My mistake is that after trying to do this for a single record during the edit session, I assumed that it is not working at all and did not try to use VB parser in a field calculator.
0 Kudos