Label Multiple Fields but Omit Null/Specific Value

1580
4
03-23-2020 06:07 AM
BenjaminSmith_McRobie2
New Contributor

Hello All,

I am setting up manhole points with multiple labels (Manhole Number, Rim Elev, Inverts, etc.). I want to know if there is any possible way to write an expression that will omit/remove Null or specific values (such as "0"). I have screenshots below and a copy of my expression.

Thank you in advance! 

Expression:

   [ "MH#: " & [MH_ID] & vbNewLine & "RIM: " & [RIM_ELEV] & vbNewLine & "INV 1: " & [INV_1] & vbNewLine & "INV 2: " & [INV_2] & vbNewLine & "INV 3: " & [INV_3] & vbNewLine & "INV 4: " & [INV_4] ]

0 Kudos
4 Replies
JakeSkinner
Esri Esteemed Contributor

Hey Benjamin,

I can provide you an example of this using Python:

def FindLabel ( [NAME], [FCC] ):
    if [FCC] == '0' or [FCC] == None:
        return [NAME]
    else:
        return [NAME] + '\n' + [FCC]

The statement is saying if the value of field FCC equals 0 or NULL, return only the value of field NAME.  Else, return NAME and the FCC field values.  Here is an example using Pro:

JoshuaSharp-Heward
Occasional Contributor III

Hi,

 

I'm not familiar with VBScript, but this can be done relatively easily in Python, as long as you check the "Advanced" box in the Label expression window.

 

Example code:

def FindLabel ( [test], [test2] , [test4], [test3] 😞
  test1, test2, test3, test4 = [test], [test2] , [test4], [test3] # assign variables for each of the input fields
  returnstring = "" #empty string for label
  for field in [test1, test2, test3, test4]: # looping through all input fields
    if field is not None and field !="0": # checks field for your conditions, e.g. not null and not "0"
      returnstring += field + '\n' #adds field values to return string if above condition met
  return returnstring # returns label string

 

Writing it this way will work for any number of fields you have, assuming the logic is the same for each (excluding null values and "0"'s.

Hope this helps!

BenjaminSmith_McRobie2
New Contributor

All of your comments were extremely helpful, thank you!! 

0 Kudos
WendyHarrison
Esri Contributor

Hi Benjamin, 

Going through the labeling expression to do this will slow the labeling down, as each time it goes to label it runs the expression.  The preferred way will be to narrow down what is labeled using a SQL query.

You can see in the screenshot below an example where there is a feature with a MyNumber value of 0 and one with a NULL value.  The SQL query removes those and only the remaining features are labeled with the objectid field.

If you are not familiar with writing SQL queries please click the Help button on the dialog box and it will take you to the documentation - there are many examples included that will help you out with different scenarios.