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

2 Answers 2

up vote 4 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

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.