A LIKE
predicate is used to search for character strings that have a particular pattern. This pattern can be a certain character string or any sequence of characters (whose length may or may not be known).Syntax
<like_predicate> ::= <expression> [NOT] LIKE <like_expression> [ESCAPE <expression>]
<like_expression> ::= <expression> | '<pattern_element>...'
expression, pattern_elementExplanation
The expression in the
like_expression must supply an alphanumeric value or a date or time value.x NOT LIKE y
has the same result as NOT(x LIKE y).
Result of x LIKE y |
|
x or y are NULL values |
x LIKE y is undefined |
x and y are non-NULL values |
x LIKE y is either true or false |
x can be split into substrings with the result that: A substring of x is a sequence of 0,1, or more contiguous characters, and each character of x belongs to exactly one substring. The number of substrings of x and y is identical. If the nth pattern element of y is a set of characters and the nth substring of x is a single character that is contained in the set of characters. |
x LIKE y is true |
x can be split into substrings with the result that: A substring of x is a sequence of 0,1, or more contiguous characters, and each character of x belongs to exactly one substring. The number of substrings of x and y is identical. If the nth pattern element of y is a sequence of characters and the nth substring of x is a sequence of 0 or more characters. |
x LIKE y is true |
If ESCAPE is specified, the corresponding expression in the LIKE predicate must supply an alphanumeric value that consists of just one character. If this escape character is contained in the LIKE expression, the following character is regarded as an independent character.
An escape character must be used if a search is to be performed for an <underscore>, '?', '%' or '*', or the hexadecimal value X'1E' or X'1F'.
Search for any character string with a minimum length of 1:
Model table:
customerCustomers whose name ends with 'FT':
SELECT name, city FROM customer
WHERE name LIKE '%FT'
NAME |
CITY |
DATASOFT |
Dallas |
Finding all customers whose names consist of six letters and begin with 'P':
SELECT name, city FROM customer
WHERE name LIKE 'P?????'
NAME |
CITY |
Porter |
Los Angeles |
Peters |
Los Angeles |
Finding all customers whose names have any lengths and begin with 'M':
SELECT name, city FROM customer
WHERE name LIKE 'M%'
NAME |
CITY |
Martin |
Los Angeles |
Masterstone |
Los Angeles |
Moore |
New York |
Melson |
Los Angeles |
Finding customers whose name contains an 'o' after the first letter:
SELECT name, city FROM customer
WHERE name LIKE '_%o%'
NAME |
CITY |
Porter |
New York |
Randolph |
Los Angeles |
Brown |
Los Angeles |
Jackson |
Los Angeles |