SQL Wildcard Operations
Explanation
A wildcard is used to replace one or more characters in a string.
Wildcards are useful in situations when incomplete information is available and it would be impossible to write a WHERE
clause using one of the existing logical operators =, <, >, ≤ or ≥.
Wildcard characters are used with the SQL LIKE
operator. The LIKE
operator is used in a WHERE
clause to perform search operations.
Two different wildcards can be used:
% (percentage synbol) is used to represent zero, one or multiple characters - (a dash) is used to represent a single character
The following are some examples of LIKE
used with the wildcards:
Example | Purpose |
---|---|
WHERE surname LIKE Thom% |
Used to find any values in the surname field that start with Thom |
WHERE surname LIKE %son |
Used to find any values in the surname field that end with son |
WHERE surname LIKE %is% |
Used to find any values that have is anywhere in the surname field |
WHERE surname LIKE _h% |
Used to find any values in the surname field that have h as the second character |
WHERE surname LIKE m__ |
Used to find any values in the surname field that start with m and have at least 3 characters |
WHERE surname LIKE a%z |
Used to find any values in the surname field that that start with a and end with z |
Use case example:
1 2 3 |
|