Black friday

Save big!

All courses under $5 - for a limited time!

Code has been added to clipboard!

SQL Wildcard Characters With Query Examples

Reading time 2 min
Published Aug 9, 2017
Updated Oct 3, 2019

SQL Wildcard: Main Tips

  • Wildcards have the same purpose as Regular Expressions.
  • A wildcard character is an alternative character replacing certain other character(s) in a string.
  • SQL wildcards are useful when you want to perform a faster search for data in a database.
  • MS Access uses a question mark (?) instead of an underscore (_).
  • [charlist], [^charlist] and [!charlist] can be used in SQL Server and MS Access.
  • You can combine several wildcards.

Wildcard Characters

%

An alternate for more than zero characters.

This example selects all developers with a City starting with 'pa'.

Example
SELECT * FROM Developers
WHERE City LIKE 'pa%';

This example selects all developers with a City containing the pattern 'am'.

Example
SELECT * FROM Developers
WHERE City LIKE '%am%';

_

An alternate for a single character.
This example selects all developers with a City starting with any character, followed by 'ondon'.

Example
SELECT * FROM Developers
WHERE City LIKE '_ondon';

This example selects all developers with a City starting with 'D', followed by any character, followed by 'l', followed by any character, followed by 'i'.

Example
SELECT * FROM Developers
WHERE City LIKE 'D_l_i';

[charlist]

Specifies ranges of characters and sets to match.
This example selects all developers with a City starting with 'n', 'l', or 'm'.

Example
SELECT * FROM Developers
WHERE City LIKE '[nlm]%';

This example selects all developers with a City starting with 'a', 'b', 'c' or 'd'.

Example
SELECT * FROM Developers
WHERE City LIKE '[a-d]%';

[^charlist] and [!charlist]

Specifies ranges of characters and sets NOT to match.
This example selects all developers with a City NOT starting with 'p', 'd', or 'm'.

Example
SELECT * FROM Developers
WHERE City LIKE '[!pdm]%';

Using LIKE Operators with _ and % Wildcards

WHERE DeveloperName LIKE 's%'
Searches for a value where 's' is the first symbol.

WHERE DeveloperName LIKE '%s'
Searches for a value where 's' is the last symbol.

WHERE DeveloperName LIKE '%on%'
Searches for a value which has 'on' in it.

WHERE DeveloperName LIKE '_n%'
Searches for a value where 'n' is the second symbol

WHERE DeveloperName LIKE 'b_%_%'
Searches for a value where 'b' is the first character and the string is at least 3 characters long.

WHERE DeveloperName LIKE 't%s'
Searches for a value where 't' is the first symbol and 's' is the last symbol.

SQL Wildcard: Summary

  • Wildcard characters work the same as Regular Expressions.
  • You can use several wildcards in a single string.
  • Some databases may use a different SQL wildcard for the same function.