3

I have two fields that I want to concatenate. Field1 contains null values and Field2 has values for every record. I want to concatenate only when both fields have values. I tried using a function with an if/else statement but that doesn't work as the resulting concatenation will return values from field2.

def concat_fields(field1, field2):
    if field1 == ""
        return 

    else:
        return field1 + field2

I know this is wrong. Any suggestions?

EDIT:

enter image description here

1
  • If one field is empty do you want to return an empty string or the unconcatenated value of the other field? I would have thought the latter.
    – PolyGeo
    Commented Mar 7, 2013 at 11:47

3 Answers 3

5

Based on your results, it looks like field1 may have a space (" ") in the field, instead of an empty string.

def concat_fields(field1, field2):
    if field1.strip() == "" or field2.strip() == "":
        return ""
    else:
        return field1 + field2
2
  • Great, this worked. But I had to return an empty string - return "" - for it to work as I was getting an error The field is not nullable
    – dchaboya
    Commented Mar 6, 2013 at 21:04
  • Thanks, I modified the code on my end to return the empty string.
    – raykendo
    Commented Mar 6, 2013 at 21:07
0

If you are mean empty strings "" instead of NULL, you are missing just a 'or' operator:

def concat_fields(field1, field2):
  if field1 == "" or field2 == "":
      return 

  else:
      return field1 + field2
1
  • hmmm, i'm still getting values from field2 even if field1 has empty strings. The expression i'm using is concat_fields(!FIELD1!, !FIELD2!)
    – dchaboya
    Commented Mar 6, 2013 at 14:03
0

I've the same problem. I've two fields to concatenate, exluding empty values. The fields are: AREA_HI AREA_LO

But arcgis return an error "000539 : Error message from Python."

Some suggestions? Thank's.

def concat_fields(!AREA_HI!, !AREA_LO!):
    if !AREA_HI!.strip() == "" or !AREA_LO!.strip() == "":
        return ""
    else:
        return !AREA_HI! + !AREA_LO!
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.