Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
    
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

3 Answers 3

up vote 5 down vote accepted

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
share|improve this answer
    
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

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
share|improve this answer
    
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

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!
share|improve this answer
    
Please consider posting your question as a new question to give it more exposure, rather than in an answer to an existing question. This might help you: gis.stackexchange.com/questions/8695/… –  nmpeterson Oct 20 at 18:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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