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]%';
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 19/01/2021
3176 People Used
Only 97 Left
Rating
5.0