Wildcards are special characters used to match parts of a value in the where clause.
To use wildcards in search clauses, the LIKE operator must be used.
Wildcard searching can be used only with text fields (strings).
There are two wildcards often used in conjunction with the LIKE operator:
% - Means match any number of occurrences of any character.
_ - The underscore represents a single character
SELECT column1, column2, ... FROM table_name WHERE columnN LIKE pattern; |
You can also combine any number of conditions using AND or OR operators.
Here are some examples showing different LIKE operators with '%' and '_' wildcards.
LIKE Operator | Description |
---|---|
WHERE StudentName LIKE 'b%' | Finds any values that start with "b" |
WHERE StudentName LIKE '%ed%' | Finds any values that have "ed" in any position |
WHERE StudentName LIKE 'a_%' | Finds any values that start with "a" and are at least 2 characters in length |
WHERE StudentName LIKE 'b%t' | Finds any values that start with "b" and ends with "t" |
WHERE StudentName LIKE '%t' | Finds any values that end with "t" |
WHERE StudentName LIKE '_a%' | Finds any values that have "a" in the second position |
WHERE StudentName LIKE 's__%' | Finds any values that start with "s" and are at least 3 characters in length |
Below is a selection from the "Product" table in the DB2 database.
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7005 | Honda | Bikes | 1,200 | ||
7006 | PEN | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
The following SQL statement selects all Product with a ProductName starting with "H":
SELECT * FROM Product WHERE ProductName LIKE 'H%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7005 | Honda | Bikes | 1,200 |
The following SQL statement selects all Product with a ProductName ending with "e":
SELECT * FROM Product WHERE ProductName LIKE '%e'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7007 | Cddrive | Accessories | 75.00 |
The following SQL statement selects all Product with a ProductName that have "dd" in any position:
SELECT * FROM Product WHERE ProductName LIKE '%dd%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7002 | Harddrive | 65.00 | 20 | 1,300 | |
7007 | Cddrive | Accessories | 75.00 |
The following SQL statement selects all Product with a ProductName that have "o" in the second position:
SELECT * FROM Product WHERE ProductName LIKE '_o%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7005 | Honda | Bikes | 1,200 |
The following SQL statement selects all Product with a ProductName that starts with "H" and ends with "e":
SELECT * FROM Product WHERE ProductName LIKE 'H%e';; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7002 | Harddrive | 65.00 | 20 | 1,300 |
The following SQL statement selects all Product with a ProductName that does NOT start with "H":
SELECT * FROM Product WHERE ProductName NOT LIKE 'H%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7004 | RAM | Components | 23.50 | 16 | 376.00 |
7006 | PEN | 7.45 | 10 | 74.50 | |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!