LIKE

The LIKE operator returns TRUE if a string matches a given mask.

Syntax

expr [NOT] LIKE mask [ ESCAPE "char" ]
  1. expr is any character string expression.
  2. mask is a character string expression defining the filter.
  3. char is a single char specifying the escape symbol.
  4. The NOT keyword negates the comparison.

Usage

The mask can be any combination of characters, including the % and _ wildcards:

  • The % percent character matches any string of zero or more characters.
  • The _ underscore character matches any single character.

Trailing blanks in the expression are not significant. Consequently, 'a ' LIKE '_' is TRUE, but ' ' LIKE '_' is FALSE.

The ESCAPE clause can be used to define an escape character different from the default backslash. It must be enclosed in single or double quotes.

A backslash (or the escape character specified by the ESCAPE clause) makes the operator treat the next character as a literal character, even if it is one of the special symbols in the mask list. This allows you to search for %, _ or \ characters.

Do not confuse with the LIKE clause of the DEFINE instruction.

LIKE is also an SQL operator evaluated by the database server. Such expression may have a different behavior than the LIKE operator of the language.

If you need to escape a wildcard character, keep in mind that a string constant must also escape the backslash character. As a result, if you want to pass a backslash to the LIKE operator (by using backslash as default escape character), you need to write four backslashes in the original string constant.

The next table shows some examples of string constants used in the source code and their equivalent LIKE pattern:

Table 1. Examples of string constants used in the source code and their equivalent LIKE pattern
Original String Constant Equivalent LIKE pattern Description
"%" % Matches any character in a non-empty string.
"_" _ Matches a single character.
"abc%" abc% Starts with abc.
"*abc" %abc Ends with abc.
"%abc%" %abc% Contains abc.
"abc__" abc__ Strings equals abc followed by two additional characters.
"\\%" \% Contains a single star character (the % wildcard is escaped)
"%abc\\\\def%" %abc\\def% Contains abc followed by a backslash followed by def (the backslash is escaped)

Example

MAIN
  IF "abcdef" LIKE "a%e_" THEN
     DISPLAY "The value matches."
  END IF
END MAIN