ASA SQL User's Guide
Queries: Selecting Data from a Table
The WHERE clause: specifying rows
The LIKE keyword indicates that the following character string is a matching pattern. LIKE is used with character, binary, or date and time data.
The syntax for LIKE is:
{ WHERE | HAVING } expression [ NOT ] LIKE match-expression
The expression to be matched is compared to a match-expression that can include these special symbols:
Symbols | Meaning |
---|---|
% | Matches any string of 0 or more characters |
_ | Matches any one character |
[specifier] |
The specifier in the brackets may take the following forms:
|
[^specifier] | The caret symbol (^) preceding a specifier indicates non-inclusion. [^a-f] means not in the range a-f; [^a2bR] means not a, 2, b, or R. |
You can match the column data to constants, variables, or other columns that contain the wildcard characters displayed in the table. When using constants, you should enclose the match strings and character strings in single quotes.
All the following examples use LIKE with the last_name column in the Contact table. Queries are of the form:
SELECT last_name FROM contact WHERE last_name LIKE match-expression
The first example would be entered as
SELECT last_name FROM contact WHERE last_name LIKE 'Mc%'
Match expression | Description | Returns |
---|---|---|
'Mc%' | Search for every name that begins with the letters Mc | McEvoy |
'%er' | Search for every name that ends with er | Brier, Miller, Weaver, Rayner |
'%en%' | Search for every name containing the letters en. | Pettengill, Lencki, Cohen |
'_ish' | Search for every four-letter name ending in ish. | Fish |
'Br[iy][ae]r' | Search for Brier, Bryer, Briar, or Bryar. | Brier |
'[M-Z]owell' | Search for all names ending with owell that begin with a single letter in the range M to Z. | Powell |
'M[^c]%' | Search for all names beginning with M' that do not have c as the second letter | Moore, Mulley, Miller, Masalsky |
Wildcard characters used without LIKE are interpreted as literals rather than as a pattern: they represent exactly their own values. The following query attempts to find any phone numbers that consist of the four characters 415% only. It does not find phone numbers that start with 415.
SELECT phone FROM Contact WHERE phone = '415%'
You can use LIKE on date and time fields as well as on character data. When you use LIKE with date and time values, the dates are converted to the standard DATETIME format, and then to VARCHAR.
One feature of using LIKE when searching for DATETIME values is that, since date and time entries may contain a variety of date parts, an equality test has to be written carefully in order to succeed.
For example, if you insert the value 9:20 and the current date into a column named arrival_time, the clause:
WHERE arrival_time = '9:20'
fails to find the value, because the entry holds the date as well as the time. However, the clause below would find the 9:20 value:
WHERE arrival_time LIKE '%09:20%'
With NOT LIKE, you can use the same wildcard characters that you can use with LIKE. To find all the phone numbers in the Contact table that do not have 415 as the area code, you can use either of these queries:
SELECT phone FROM Contact WHERE phone NOT LIKE '415%' SELECT phone FROM Contact WHERE NOT phone LIKE '415%'