How To: Calculate fields where some inputs are null values
These instructions provide examples of VBScript and Python syntax that show a way to calculate fields where some input values are null.
Be aware that the Python form of the script produces the expected results on 10.1, but on 10.0 does not return a value for null inputs. VBScript produces the correct results on both versions.
The calculation in each case applies the source value (in this case, the field Dbl1 or Dbl2) to the target field unless the source is null, in which case the target field is assigned the value zero.
1) Copy and paste the following sample into a Notepad file and save it with a .cal extension.
2) Load the .cal file into the Field Calculator to calculate the desired target field, setting the parser to VBScript. Substitute the desired field data source for field ‘Dbl1’ in the sample.
if IsNull ([Dbl1]) then
X = 0
X = [Dbl1]
1) Copy and paste this sample into a Notepad file with a .cal extension.
2) Load the .cal file into the Field Calculator to calculate the desired target field, setting the parser to Python or Python_9.3. Substitute the desired field data source for field ‘Dbl2’ in the sample.
def X (param):
retVal = 0
if param is not None:
retVal = param
See the following illustrations of a table before and after each field calculation. In the table, field Dbl1Calc was calculated using the sample VBScript code. Field Dbl2Calc was calculated using the sample Python code.