bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/SQL/SQL Tutorial
SQL•SQL Tutorial

SQL Wildcards

SQL Wildcard Characters

A wildcard character is used to substitute one or more characters in a string.

Wildcard characters are used with the LIKE operator. The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

Example

SELECT * FROM Customers

WHERE CustomerName LIKE 'a%';

Wildcard Characters

SymbolDescription
%Represents zero or more characters
_Represents a single character
[]Represents any single character within the brackets *
^Represents any character not in the brackets *
-Represents any single character within the specified range *
{}Represents any escaped character **
  • Not supported in PostgreSQL and MySQL databases.

** Supported only in Oracle databases.

Demo Database

Below is a selection from the Customers table used in the examples:

CustomerIDCustomerNameContactNameAddressCityPostalCodeCountry
1Alfreds FutterkisteMaria AndersObere Str. 57Berlin12209Germany
2Ana Trujillo Emparedados y heladosAna TrujilloAvda. de la Constitución 2222México D.F.05021Mexico
3Antonio Moreno TaqueríaAntonio MorenoMataderos 2312México D.F.05023Mexico
4Around the HornThomas Hardy120 Hanover Sq.LondonWA1 1DPUK
5Berglunds snabbköpChristina BerglundBerguvsvägen 8LuleåS-958 22Sweden

Using the % Wildcard

The % wildcard represents any number of characters, even zero characters.

Example

SELECT * FROM Customers

WHERE CustomerName LIKE '%es';

Example

SELECT * FROM Customers

WHERE CustomerName LIKE '%mer%';

Using the _ Wildcard

The _ wildcard represents a single character.

It can be any character or number, but each _ represents one, and only one, character.

City
City

Using the [] Wildcard

The [] wildcard returns a result if any of the characters inside gets a match.

Example

SELECT * FROM Customers

WHERE CustomerName LIKE '[bsp]%';

Using the - Wildcard

The - wildcard allows you to specify a range of characters inside the [] wildcard.

Example

SELECT * FROM Customers

WHERE CustomerName LIKE '[a-f]%';

Combine Wildcards

Any wildcard, like % and _ , can be used in combination with other wildcards.

Example

SELECT * FROM Customers

 WHERE CustomerName LIKE 'a__%';

Example

SELECT * FROM Customers

 WHERE CustomerName LIKE '_r%';

Without Wildcard

If no wildcard is specified, the phrase has to have an exact match to return a result.

Example

SELECT * FROM Customers

 WHERE Country
 LIKE 'Spain';

Microsoft Access Wildcards

The Microsoft Access Database has some other wildcards:

SymbolDescriptionExample
*Represents zero or more charactersbl* finds bl, black, blue, and blob
?Represents a single characterh?t finds hot, hat, and hit
[]Represents any single character within the bracketsh[oa]t finds hot and hat, but not hit
!Represents any character not in the bracketsh[!oa]t finds hit, but not hot and hat
-Represents any single character within the specified rangec[a-b]t finds cat and cbt
#Represents any single numeric character2#5 finds 205, 215, 225, 235, 245, 255, 265, 275, 285, and 295

Previous

SQL LIKE Operator

Next

SQL IN Operator

This chapter

Overview
46

Lessons

115m

Read time

1. SQL Tutorial2. Introduction to SQL3. SQL Syntax4. SQL SELECT Statement5. SQL SELECT DISTINCT Statement6. SQL WHERE Clause7. SQL ORDER BY Keyword8. SQL AND Operator9. SQL OR Operator10. SQL NOT Operator11. SQL INSERT INTO Statement12. SQL NULL Values13. SQL UPDATE Statement14. SQL DELETE Statement15. SQL SELECT TOP, LIMIT and FETCH FIRST16. SQL Aggregate Functions17. SQL MIN() F unction18. SQL MAX() Function19. SQL COUNT() Function20. SQL SUM() Function21. SQL AVG() Function22. SQL LIKE Operator23. SQL Wildcards24. SQL IN Operator25. SQL BETWEEN Operator26. SQL Aliases27. SQL Joins28. SQL INNER JOIN29. SQL LEFT JOIN30. SQL RIGHT JOIN31. SQL FULL JOIN32. SQL Self Join33. SQL UNION Operator34. SQL UNION ALL Operator35. SQL GROUP BY Statement36. SQL HAVING Clause37. SQL EXISTS Operator38. SQL ANY Operator39. SQL ALL Operator40. SQL SELECT INTO Statement41. SQL INSERT INTO SELECT Statement42. SQL CASE Expression43. SQL NULL Functions44. SQL Stored Procedures45. SQL Comments46. SQL Operators

On this page

SQL Wildcard CharactersWildcard CharactersDemo DatabaseUsing the % WildcardUsing the _ WildcardUsing the [] WildcardUsing the - WildcardCombine WildcardsWithout WildcardMicrosoft Access Wildcards