ArcGIS Desktop – Using If/Then Statement with LIKE in Field Calculator

arcgis-desktopfield-calculatorif statementvbscript

I cannot get the below to work with a LIKE variable within the If/Then statement. Is there something that I am missing?

Prelogic Script

dim y

If [Categories] LIKE '%Orange%' Then
y = "Medium"
Else y = [Categories] 
End If

Status = 
y

Best Answer

There is no LIKE comparison operator in VB Script (see link).

You could use InStr to check if some search text appears somewhere in the input string:

dim y

If InStr([Categories], "Orange") Then
  y = "Medium"
Else
  y = [Categories] 
End If

Status = y

InStr will return 0 if the search text is not found, which will be interpreted as False in the condition statement.

If you want to search some text explicitly in the begining of the input string, just check if the value returned from InStr is 1.

If you want to search some text in the end of the input string, try Right function to extract part of the string and then do the comparison.