The pattern matching operator LIKE compares a string value to a pattern of characters that is also a string, for example, 'abc' LIKE '???'. If the string value matches the pattern, the result of the operation is TRUE. Otherwise, the result of the operation is FALSE. The pattern matching operation is case-sensitive.
Patterns are created by combining single characters, character ranges, or both. Character ranges are specified by concatenating the first character in the range, a hyphen, and the last character in the range, for example, 0-9. Valid characters include numbers, uppercase and lowercase letters, and reserved characters that have special meanings. Character sets contain one or more characters, character ranges, or both and are enclosed by square brackets. For example, [0-9A-Za-z], [0-2ABC], and [789x-z] are all valid character sets.
Note: Character ranges are always part of a character set.
The following table lists the reserved characters and their meanings:
Character |
Meaning |
---|---|
' or " |
Specifies a string, such as 'abc' or "abc" |
- |
Specifies a range of characters, such as 0-9 |
? |
Matches any single character |
* |
Matches any sequence of characters, including one or none |
[set] |
Matches any single character in the specified set |
[!set] or [^set] |
Matches any single character not in the specified set |
[set]? |
Matches any single character in the specified set or an empty string |
[set]* |
Matches any sequence of characters in the specified set, including one or none |
\ |
Treats any reserved character as a regular character, such as \* |
Examples that use '?'
'' LIKE '?'
Result = FALSE
'a' LIKE '?'
Result = TRUE
'ab' LIKE '?'
Result = FALSE
'abc' LIKE '???'
Result = TRUE
'191' LIKE '1??'
Result = TRUE
'201' LIKE '1??'
Result = FALSE
Examples that use '*'
'' LIKE '*'
Result = TRUE
'a' LIKE '*'
Result = TRUE
'a1b2c3' LIKE '*'
Result = TRUE
'robin' LIKE 'r*n'
Result = TRUE
'room' LIKE 'r*n'
Result = FALSE
Examples that use '[set]'
'' LIKE '[abcde]'
Result = FALSE
'f' LIKE '[abcde]'
Result = FALSE
'c' LIKE '[abcde]'
Result = TRUE
'abc' LIKE '[abcde]' Compare: 'abc' LIKE '[abcde]*'
Result = FALSE
Examples that use '[!set]' or '[^set]'
'' LIKE '[!abcde]'
Result = FALSE
'f' LIKE '[^abcde]'
Result = TRUE
'c' LIKE '[!abcde]'
Result = FALSE
'xyz' LIKE '[^abcde]'
Result = FALSE
Examples that use '[set]?'
'' LIKE '[abcde]?'
Result = TRUE
'a' LIKE '[abcde]?'
Result = TRUE
'ab' LIKE '[abcde]?'
Result = FALSE
'z' LIKE '[abcde]?'
Result = FALSE
Examples that use '[set]*'
'' LIKE '[abcde]*'
Result = TRUE
'a' LIKE '[abcde]*'
Result = TRUE
'aabbccddee' LIKE '[abcde]*'
Result = TRUE
'abcdef' LIKE '[abcde]*'
Result = FALSE
'abc' LIKE '[abcde]*' Compare: 'abc' LIKE '[abcde]'
Result = TRUE
Examples that use '\'
'123-456-7890' LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
Result = TRUE
'_!_^_*_?_' LIKE '_\!_\^_\*_\?_'
Result = TRUE
Examples that test case-sensitivity
'a' LIKE '[a-z]'
Result = TRUE
'A' LIKE '[a-z]'
Result = FALSE
'A' LIKE '[A-Za-z]'
Result = TRUE
'Robin' LIKE '[A-Za-z]*'
Result = TRUE
'Robin' LIKE '[A-Z][a-z]*'
Result = TRUE
'robin' LIKE '[A-Z][a-z]*'
Result = FALSE
Copyright © 2010 CA. All rights reserved. | Email CA about this topic |