Wildcards

Wildcards allow one or more characters to be ignored in a text comparison (see Text comparisons).

For example, using the Employee table,

SELECT FirstName, LastName FROM Employee WHERE FirstName = "Julia";

returns an exact match only - if there was a "Julian" in the table, it would not be returned by the above SELECT.

To get around this, we can use the LIKE operator in conjunction with an asterisk "*" (the wildcard):

SELECT FirstName, LastName FROM Employee WHERE FirstName LIKE "J*";

This produces a match on any FirstName beginning with the letter "J", in this case "Julia" and "Jim" would be returned.

To find any first name containing the letter "h" use the asterisk twice:

SELECT FirstName, LastName FROM Employee WHERE FirstName LIKE "*h*";

This returns "Phil" and "Chris".

Another wildcard is the question mark, "?". This can be used to replace a single, unknown letter in a condition:

SELECT FirstName, LastName FROM Employee WHERE FirstName LIKE "?h*";

This would match "Phil" and "Chris" again, whereas "??i*" would only match "Phil".

See also:

Next topic - Selecting blank entries

Selecting records - the basics

Other types of SQL statements