A wildcard character is used to substitute one or more characters in a string.
Wildcard characters are used with the SQL LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.
Use the following wildcard characters to search for and list DB2 objects:
_ (underscore) matches any single character.
% (percent sign) or * (asterisk) matches a string of zero or more characters.
Wildcards can also be used in combinations.
Here are some examples showing different LIKE operators with '%' and '_' wildcards:
LIKE Operator | Description |
---|---|
WHERE StudentName LIKE 's%' | Finds any values that starts with "s" |
WHERE StudentName LIKE '%i' | Finds any values that ends with "i" |
WHERE StudentName LIKE '%sp%' | Finds any values that have "sp" in any position |
WHERE StudentName LIKE '_v%' | Finds any values that have "v" in the second position |
WHERE StudentName LIKE 's_%_%' | Finds any values that starts with "s" and are at least 3 characters in length |
WHERE StudentName LIKE 's%p' | Finds any values that starts with "s" and ends with "p" |
The following predicate is true when the string to be tested in NAME has the value SMITH, NESMITH, SMITHSON, or NESMITHY. It is not true when the string has the value SMYTHE:
STUDENT_NAME LIKE '%SMITH%' |
The following two predicates are equivalent; three of the four percent signs in the first predicate are redundant.
STUDENT_NAME LIKE 'SP%%%%AN' STUDENT_NAME LIKE 'SP%AN' |
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 Category starting with "Acc":
SELECT * FROM Product WHERE Category LIKE 'Acc%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
The following SQL statement selects all Product with a Category containing the pattern "or":
SELECT * FROM Product WHERE Category LIKE '%or%'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
7007 | Cddrive | Accessories | 75.00 | ||
7008 | Speaker | Accessories | 75.00 |
The following SQL statement selects all Product with a Category starting with any character, followed by "ikes":
SELECT * FROM Product WHERE Category LIKE '_ikes'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7005 | Honda | Bikes | 1,200 |
The following SQL statement selects all Product with a Category starting with "Accessorie" and end with any character.
SELECT * FROM Product WHERE Category LIKE 'Accessorie_'; |
ProductID | ProductName | Category | Price | QtyPrice | TotalValue |
---|---|---|---|---|---|
7001 | Mouse | Accessories | 75.00 | ||
7003 | Keyboard | Accessories | 36.00 | 33 | 1,118.00 |
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!