A wildcard in SQL is a special symbol used to substitute one or more characters in a string. Typically, you can apply SQL wildcard characters with the LIKE
operator in the WHERE
clause to search for a specific pattern within a column. They enable you to retrieve data even when the exact value is unknown, making it easier to perform flexible, pattern-based searches and narrow down results efficiently.
Character | Description | Example |
---|---|---|
% | Represents zero or more characters. | br% finds br, bracelet, break, and brink |
_ | Represents a single character. | f_r finds far, fir, for, and fur |
[] | Represents any single character within the brackets. | s[ia]nk finds sink and sank, but not sunk |
[^] | Represents any character but not those specified in the brackets. | s[^ia]nk finds sunk, but not sink and sank |
[-] | Represents any single character within the specified range. | s[e-o]n finds sin and son, but not sun |
Another thing worth mentioning here is that all wildcards can be used in combinations to make your search more flexible.
Let us illustrate the wildcard syntax on the AdventureWorks2019 sample database. Here are the first 15 records from the Person.Address table that we will use in our examples.
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
1 | 714 Pine Tree St | Riverside | 7 | 64467 |
2 | 1026 New Rock Hill Circle | Wilkers-Barre | 29 | 79892 |
3 | 1759 W Fox Hill Ave | Algonquin | 24 | 29820 |
4 | 2433 West Ashwood Way | Wilkers-Barre | 29 | 79892 |
5 | 282 West Burwood Cir | Anna | 50 | 39435 |
6 | 1263 Riverside Way | Anna | 50 | 39435 |
7 | 61 Mount Court | Gallup | 10 | 49517 |
8 | 61 Mount Court | Ottawa | 8 | 63180 |
9 | 105 Meadowview Road | Salem | 11 | 23395 |
10 | 1416 Burwood Highway | Anna | 50 | 39435 |
11 | 3317 White Stonewood Loop | Kaufman | 25 | 17678 |
12 | 1274 NE Oak Pkwy | Kaufman | 25 | 17678 |
13 | 574 Woodrow Way | Altus | 37 | 69585 |
14 | 1026 New Rock Hill Circle | Seaside | 26 | 67078 |
15 | 2433 West Ashwood Way | Seaside | 26 | 67078 |
With the demo database at hand, here are several examples that will show you wildcards in action.
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
9 | 105 Meadowview Road | Salem | 11 | 23395 |
14 | 1026 New Rock Hill Circle | Seaside | 26 | 67078 |
15 | 2433 West Ashwood Way | Seaside | 26 | 67078 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
10 | 1416 Burwood Highway | Anna | 50 | 39435 |
11 | 3317 White Stonewood Loop | Kaufman | 25 | 17678 |
12 | 1274 NE Oak Pkwy | Kaufman | 25 | 17678 |
13 | 574 Woodrow Way | Altus | 37 | 69585 |
14 | 1026 New Rock Hill Circle | Seaside | 26 | 67078 |
15 | 2433 West Ashwood Way | Seaside | 26 | 67078 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
1 | 714 Pine Tree St | Riverside | 7 | 64467 |
7 | 61 Mount Court | Gallup | 10 | 49517 |
9 | 105 Meadowview Road | Salem | 11 | 23395 |
14 | 1026 New Rock Hill Circle | Seaside | 26 | 67078 |
15 | 2433 West Ashwood Way | Seaside | 26 | 67078 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
1 | 714 Pine Tree St | Riverside | 7 | 64467 |
7 | 61 Mount Court | Gallup | 10 | 49517 |
8 | 61 Mount Court | Ottawa | 8 | 63180 |
11 | 3317 White Stonewood Loop | Kaufman | 25 | 17678 |
15 | 2433 West Ashwood Way | Seaside | 26 | 67078 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
7 | 61 Mount Court | Gallup | 10 | 49517 |
8 | 61 Mount Court | Ottawa | 8 | 63180 |
11 | 3317 White Stonewood Loop | Kaufman | 25 | 17678 |
12 | 1274 NE Oak Pkwy | Kaufman | 25 | 17678 |
When using SQL wildcards, especially with large datasets, optimizing your queries to avoid performance bottlenecks is important. Here are some key guidelines to help you achieve efficient wildcard searches:
'%pattern'
) forces the database to perform a full table scan, which can significantly slow down query performance. To improve efficiency, structure your queries to use trailing wildcards (for example, 'pattern%'
) so that the query engine can take advantage of indexes. Additionally, avoid using multiple leading wildcards or combining them with underscores (for example, '%_pattern'
), as these patterns prevent index usage and increase the processing load.SELECT * FROM Products WHERE Description LIKE '%computer%%soft%';
, use more focused conditions such as SELECT * FROM Products WHERE Description LIKE '%computer%' AND Description LIKE '%soft%';
to improve both performance and result relevance.'pattern%'
) allow the query engine to use indexes efficiently. However, patterns that begin with a wildcard (forexample, '%pattern'
) or include wildcards in the middle (such as 'pro%ct'
, which matches "product" or "project"), prevent index usage and often lead to full table scans. To optimize performance, index those columns that are frequently searched using prefix-based wildcards.WHERE
clause (for example, date ranges or category filters). This reduces the number of rows that need to be scanned and improves performance even when wildcards are used.When working with SQL Server wildcards, developers sometimes make mistakes that can affect query accuracy or performance. Here are some of the most common pitfalls:
'pattern%'
) to allow index optimization.=
for exact comparisons instead. For example, instead of LIKE '%pattern%'
, use = 'pattern'
.LIKE 'J__'
will only match strings with exactly three characters, such as "Jon," but not "John" or "Jo."ESCAPE
keyword. For example, to include "100% Cotton" in your search, use SELECT * FROM Products WHERE Name LIKE '%100\% Cotton%' ESCAPE '\';
.Avoid these mistakes to ensure your wildcard search in SQL is accurate and efficient.
Let's explore more complex SQL wildcard queries. To demonstrate these effectively, we have expanded the demo database with additional records to ensure meaningful results. Due to its size, we cannot provide the full demo database here, but we will share the query results—limited to no more than five records—so you can still follow along and understand the outcome.
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
22448 | 130 North Main St. | Newcastle | 50 | 2300 |
12770 | 130 North Main St. | San Diego | 9 | 92102 |
11380 | 165 North Main | Austin | 73 | 78701 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
30 | 9707 Coldwater Drive | Orlando | 15 | 32804 |
31 | 9100 Sheppard Avenue North | Ottawa | 57 | K4B 1T7 |
228 | 9241 St George Dr. | Everett | 79 | 98201 |
229 | 3928 San Francisco | Everett | 79 | 98201 |
230 | 475 Santa Maria | Everett | 79 | 98201 |
AddressID | AddressLine1 | City | StateProvinceID | PostalCode |
---|---|---|---|---|
26 | 5725 Glaze Drive | San Francisco | 9 | 94109 |
248 | 1400 Gate Drive | Newport Hills | 79 | 98006 |
252 | 9687 Shakespeare Drive | Newport Hills | 79 | 98006 |
995 | 4460 Newport Center Drive | Newport Beach | 9 | 92625 |
11931 | 7561 Humphrey Drive | Newton | 7 | V2M1P1 |
AddressID | AddressLine1 | City | StateName | TerritoryID |
---|---|---|---|---|
21094 | 1006 Deercreek Ln | Torrance | California | 4 |
19128 | 1045 Lolita Drive | Torrance | California | 4 |
15652 | 1260 Mt. Washington Way | Victoria | British Columbia | 6 |
13224 | 1315 Norse Drive | Victoria | British Columbia | 6 |
19253 | 133 Lorie Ln. | Victoria | British Columbia | 6 |
AddressID | AddressLine1 | City | StateName | TerritoryID |
---|---|---|---|---|
27683 | 1020 Carletto Drive | Santa Cruz | California | 4 |
23031 | 1065 Almond St. | Santa Cruz | California | 4 |
770 | 123 Union Square South | New York | New York | 2 |
19172 | 1583 Westwood Lane | Santa Cruz | California | 4 |
20356 | 1644 Alicante Court | Santa Cruz | California | 4 |
You can apply wildcards during your daily work with SQL queries in dbForge SQL Complete, an add-in for SSMS and Visual Studio that focuses on code completion, formatting, and refactoring. Additionally, you can use wildcard expansion to quickly insert a list of columns, available in your tables and views, into your SELECT statement.
We use cookies to provide you with a better experience on the Devart website. You can read more about our use of cookies in our Cookies Policy.
Click OK to continue browsing the Devart site. Be aware you can disable cookies at any time.