Using the Percent Sign (%) wildcard
Which cities are in districts that begin with the letters Min?
SELECT district, hq_city FROM aroma.market WHERE district LIKE 'Min%';
Previous queries have expressed conditions that match complete character strings. With the LIKE predicate and the two wildcard characters, the percent sign (%) and the underscore (_), you can also express conditions that match a portion of a character string (a substring).
The percent (%) wildcard matches any character string. For example:
like 'TOT%'is true for any string that begins with 'TOT'.
like '%ZERO%'is true for any string that contains the text 'ZERO'.
like '%FRESH'is true for any string that ends with 'FRESH' and does not contain trailing blanks. Trailing blanks in character data are deemed significant when LIKE constraints are applied.
The percent sign (%) can also be used to search for a null character string--zero (0) characters.
The underscore wildcard (_) matches any one character in a fixed position. For example:
like '_EE_'is true for any four-letter string whose two middle characters are 'EE'.
like '%LE_N%'is true for any string that contains the pattern 'LE_N'. The strings 'CLEAN', 'KLEEN', and 'VERY KLEEN' all match this pattern.
The example query retrieves the names of all districts that begin with the characters 'Min' and lists the cities in these districts. The wildcard percent sign (%) allows for any character combination (including blank spaces) after the 'n' in 'Min', but characters that precede the 'n' must match the character pattern exactly as stored.
A LIKE condition is true when its pattern matches a substring in a column. If the pattern contains no wildcard characters, the pattern must match the column entry exactly.
For example, the following condition is true only when the column entry contains the character string APRIL and nothing else:
month LIKE 'APRIL'
In other words, this condition is equivalent to:
month = 'APRIL'
The LIKE predicate can be used only on columns that contain character strings.