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

  • 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 Mar 7 '13 at 11:47
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
  • 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 Mar 6 '13 at 21:04
  • Thanks, I modified the code on my end to return the empty string. – raykendo Mar 6 '13 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
  • 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 Mar 6 '13 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!

Your Answer

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

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