How to Use Wild Cards in SQL Server
% - A substitute for zero or more characters.
_ - A substitute for exactly one character.
We will show an example of both using our Movies table.
The % Wildcard
The % wildcard searches the database for matches of zero or more characters. We will look for titles that start with ‘so’.
SELECT * FROM Movies
WHERE Title LIKE 'so%'
If you’re ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
We can also use a variation that looks for titles that contains specified letters. We will look for directors that contain the patter ‘ur’ in their name.SELECT * FROM Movies
WHERE Director LIKE '%ur%'
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The _ WildcardLike the previous wildcard, there are two forms of the _ wildcard. The first form looks for a word that begins with any character followed by a specified pattern as such:
SELECT * FROM Movies
WHERE Title LIKE '_op'
The second form plays off of the first one, just more in detail. We will search for titles that begin with ‘R’, followed by any character, followed by ‘n’, followed by any character, followed by ‘o’.
SELECT * FROM Movies
WHERE Title LIKE 'R_n_o'
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect’s help, we were able to avoid any headaches!
Wildcards serve as an easier way of searching wide variety of words in a database, especially in the bigger databases.Thanks for reading and make sure to download the source files to get a better understanding of how the code works.
Wildcards-SqlServer.zip
