LIKE
The LIKE operator
returns TRUE if a string matches a given mask.
Syntax
expr [NOT] LIKE mask [ ESCAPE "char" ] - expr is any character string expression.
- mask is a character string expression defining the filter.
- char is a single char specifying the escape symbol.
- The
NOTkeyword 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.
The escape character (by default, \ backslash) makes the operator treat the next
character as a literal character, whether or not it is one of the special symbols; a backslash
before an ordinary character is therefore simply removed. To match a literal backslash, escape it
with a second one.
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:
| String constant in source code | LIKE pattern in memory | 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. |
"\\%" |
\% |
Matches a single percent sign |
"a\\b%" |
a\b% |
Starts with ab (the backslash is dropped) |
"a\\\\b%" |
a\\b% |
Starts with a\b (the backslash is escaped) |
Example
MAIN
IF "abcdef" LIKE "a%e_" THEN
DISPLAY "The value matches."
END IF
END MAIN