2. Language Elements : Expressions : Operators : Like Operator and Pattern Matching
 
Share this page                  
Like Operator and Pattern Matching
The like operator lets you compare two strings to see whether they resemble each other in specific ways. To use this operator, specify a pattern (a string of characters with special formatting characters) that specifies what the compared string must look like. If the string being compared is an instance of the pattern described by the pattern string, the expression evaluates to TRUE.
The syntax for the like operation is:
charvar [not] like pattern [escape escapechar]
charvar
Specifies a character string variable
pattern
Specifies a character string literal or character string variable that can include the following special characters:
An underscore (_) matches any single character. For example, the pattern string, “_a” , would produce the matching text strings “Xa,” “aa” and “/a”.
A percent sign (%) matches any string of characters, regardless of length. For example, the pattern string, “Fred%”, would produce the matching text strings of “Fred,” “Frederick,” and “Fred S. Smith, Ph.D.”
The following statement uses “%” to test the value of “emp_name” to see if it starts with “Fred” and ends with a last name of “Smith”:
if emp_name like 'Fred% Smith' then
     msg = 'Found a guy matching the pattern';
endif;
Square brackets [ ], where each bracket is preceded by an escape character, which you define, match the corresponding character position with any of the characters in the bracketed string. The typical definition of an escape character is usually done with quotes.
The escape character can be used to escape itself, so no matter what character you select, you can use it as a character in the pattern by giving it twice, for example:
name like '..%.%' escape '.'
This phrase matches any string beginning with a period (.) and ending with a percent sign.
escapechar
Specifies a character string literal or character string variable of length 1. It is part of the escape clause, [escape escapechar], which has two functions:
You can use it to “escape” the special interpretation of the pattern matching characters “_” and “%.” For example, the following fragment matches a string of any first character and an underline as the second character:
name like '_\_' escape '\'
You can use the escape clause with square brackets to match any of the characters you specify. Typically, square brackets are treated the same as any other regular characters. However, when preceded by the escape character, the brackets define a “match-any-of-these-characters” string.
In the following example, you could use the escape clause with square brackets to match all strings ending with X, Y, or Z:
name like '%\[XYZ\]' escape '\'
As another example:
name like '_\[BC\]C%' escape '\'
This pattern matches “ABC,” “ACC,” “FCC Fairness Doctrine” and does not match “FDC Yellow #42” or “Access” (because “cc” is not uppercase).
Finally, the following example matches any string beginning with a left bracket and whose second character is “1,” “2,” “3” or a right bracket:
name like '[\[123]\]' escape '\'
The escape character cannot be followed in the pattern by any character other than underscore, percent, left or right bracket, or another escape character.
Pattern matching characters have no effect unless used with "like." For example, consider a statement that begins as follows:
if name = 'Fred%' then ...
The comparison tests the value of “name” to see whether it equals the constant literal “Fred%.” In the event that it does, the object of the condition is executed.