This morning, I proposed an introduction to the SQL LIKE operator. I would like to expand on the wildcard Characters. SQL Server provides a powerful set of wildcard characters that can be used in conjunction with the LIKE
operator to search for patterns within data. These wildcards allow you to specify partial matches, ranges, and sets of characters in your search criteria.
In this article, I will explore the different wildcard characters available in SQL Server and provide examples of how to use them with the Northwind sample database, that has been used for many years as an example database for learning SQL. It contains a set of tables that model a small business that sells products to customers. In this article, we will be using the Products
table in the Northwind database to demonstrate the use of wildcard characters.
The LIKE Operator
as mentioned in the previous article on LIKE
, the LIKE
operator is used to search for patterns within data. It is often used in conjunction with wildcard characters to provide more flexible search criteria.
Hereโs the basic syntax for using the LIKE
operator again:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
The column_name
is the name of the column you want to search in, and pattern
is the search pattern. The pattern
can include one or more wildcard characters.
1. The Percent Sign Wildcard (%)
The percent sign wildcard %
represents zero, one, or multiple characters. It is often used to search for patterns at the beginning or end of a string.
For example, letโs say we want to search for all products in the Northwind database that start with the word โChaiโ. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE 'Chai%';
This will return all products that have a ProductName
that starts with the word โChaiโ. The %
character is used to match any number of characters after the word โChaiโ.
We can also use the %
character at the end of a string to match any number of characters before a particular string. For example, letโs say we want to search for all products in the Northwind database that end with the word โCookiesโ. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE '%Cookies';
This will return all products that have a ProductName
that ends with the word โCookiesโ. The %
character is used to match any number of characters before the word โCookiesโ.
2. The Underscore Wildcard (_)
The underscore wildcard _
represents a single character. It is often used to search for patterns where a single character may vary.
For example, letโs say we want to search for all products in the Northwind database that have a ProductName
that is exactly five characters long and starts with the letter โCโ. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE 'C____';
This will return all products that have a ProductName
that starts with the letter โCโ and is exactly five characters long. The underscore character is used to match any single character.
3. The Square Brackets Wildcard ([])
The square brackets wildcard []
represents a single character from a set of characters. It is often used to search for patterns where a character can be one of several possible characters.
For example, letโs say we want to search for all products in the Northwind database that have a ProductName
that contains the letters โaโ, โeโ, or โiโ. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE '%[aei]%';
This will return all products that have a ProductName
that contains the letters โaโ, โeโ, or โiโ. The square brackets are used to specify a set of possible characters that can match.
We can also use ranges of characters within the square brackets. For example, letโs say we want to search for all products in the Northwind database that have a ProductName
that contains a digit. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE '%[a-d]';
This will return all products that have a ProductName
that ends with a,b,c or d. The range of characters is specified with a hyphen within the square brackets.
4. The Caret Wildcard (^)
The caret wildcard ^
is used to negate a character set within square brackets. It is often used to search for patterns that do not match a certain set of characters.
For example, letโs say we want to search for all products in the Northwind database that have a ProductName
that does not contain the letters โaโ, โeโ, or โiโ. We can use the following SQL statement:
SELECT * FROM Products
WHERE ProductName LIKE '%[^aei]%';
This will return all products that have a ProductName
that does not contain the letters โaโ, โeโ, or โiโ. The caret character is used to negate the set of characters within the square brackets.
Conclusion
Wildcard characters are a powerful tool in SQL Server that allow you to search for patterns within data. The percent sign %
is used to match zero, one, or multiple characters, the underscore _
is used to match a single character, the square brackets []
are used to match a single character from a set of characters, and the caret ^
is used to negate a character set within square brackets.
By using these wildcard characters in conjunction with the LIKE
operator, you can create flexible and powerful search criteria in your SQL queries.
Leave a Reply