label expression: 'if not field 1, then field 2', how is this achieved

7727
7
03-18-2014 01:44 PM
JennyBiddlecombe
New Contributor
Hi,
I am looking to create a label expression that will result in the following: display 'field 1', however if 'field 1' is blank, display 'field 2'
I am sure this can be written a number of ways to get the same result (eg other way round: display 'field 2' unless 'field 1' is not blank)
Would anyone be able to show how to do this?

thank you very much- any help would be greatly appreciated.

JB
Tags (2)
0 Kudos
7 Replies
MattEiben
Occasional Contributor
Are you trying to label an expression from with an arcpy script or as a Label Expression inside an ArcMap session?

If you're doing this within ArcMap, you'd need to use the Advanced Python Label Expression window and use a function something like this:

def FindLabel ( [field 1] , [field 2] ):
    if [field1] != "":
        return [field1]
    return [field2]
0 Kudos
KerryAlley
Occasional Contributor
If you have Null values, I'd recommend replacing them with something like an empty string "", or some other filler character.  I've never gotten Null values to play nicely with label expressions with Python or VBScript.  Then something like this works:
def FindLabel ( [FIELD1], [FIELD2] ):
  if [FIELD1] == "":
    return [FIELD2]
  elif [FIELD2] == "":
    return [FIELD1]
  else:
    return ""


Edit: Scooped!  Matt's code is tidier, too.
0 Kudos
JennyBiddlecombe
New Contributor
Hi,
thank you for your response.
yes I am trying to do this in ArcMap session, using the label expression maker.
Your previous suggestion does not seem to work as I cannot add in carriage returns?
is there anything else i can try?
JB
0 Kudos
KerryAlley
Occasional Contributor
"A label expression is limited to a single line of code unless you check the Advanced box on the Label Expression dialog box."  (From the Building Label Expressions help page)

good luck!
0 Kudos
JennyBiddlecombe
New Contributor
Hi there,
this results in only displaying field 2...
Any other suggestions?
thank you very much for your help!
JB



Are you trying to label an expression from with an arcpy script or as a Label Expression inside an ArcMap session?

If you're doing this within ArcMap, you'd need to use the Advanced Python Label Expression window and use a function something like this:

def FindLabel ( [field 1] , [field 2] ):
    if [field1] != "":
        return [field1]
    return [field2]
0 Kudos
KerryAlley
Occasional Contributor
Hi there,
this results in only displaying field 2...
Any other suggestions?
thank you very much for your help!
JB


What are the empty field values in your table?  Are they <Null>?  Spaces?  Empty?  If you're not absolutely sure, you can use a definition query builder to view the Unique Values.  The Show Values button on the Label Expression window has a view that I find less informative. 

I'm guessing you don't have empty fields ("") because that is the condition for displaying field 1 in Matt's code.  You might find (for example) that you have Null values, single spaces (" "), double spaces ("  "), or possibly a mixture of empty-looking values.  (Note that there should be two spaces between the last pair of quotes... the text formatting stripped my second space!)
0 Kudos
Zeke
by
Regular Contributor III
In Python I use None to check for null values. Try something like:

if [field1].strip() != "" AND NOT [field1] Is None:

or maybe you could use:

if len([field1].strip()) > 0:
 


Not sure what your values are, so see what works best. This is also assuming field1 is a string, not a number.
0 Kudos