0

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
4
  • Sorry I didn't format it correctly previously. I did have it formatted this way and it says, "There was a failure during processing, Check geoprocessing window for details" Commented Jul 19, 2018 at 20:06
  • 1
    Your expression is in VisualBasic (VB), but there is no like operator in VB. Like comes from SQL. You want to look for string functions in VB or switch to Python, where you have even more possibilities. Commented Jul 19, 2018 at 20:54
  • 1
    May be i was wrong, try to use * instead of % (docs.microsoft.com/de-de/dotnet/visual-basic/language-reference/…) Commented Jul 19, 2018 at 21:11
  • @AndreasMüller is right, % is SQL operator for LIKE clause. However, I would use python for the same calculation in one line, e.g., "Medium" if "Orange" in !Categories! else !Categories! Commented Jul 20, 2018 at 1:16

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.