Updated March 14, 2023
Introduction to SQL Wildcard Character
SQL wildcard character are used for specifying the pattern that should be followed by the value to be compared. We can match the string and check for its matching with different patterns using the LIKE operator in SQL, which is a logical operator that compares the string and searches for the part that satisfies and matches the pattern that is specified using a collection of various regular and wildcard characters. In this article, we will see about the matching of the values or contents of columns and variables using the LIKE operator and wildcard characters for pattern specification, its syntax, some of the wildcard characters and specification method and some of the examples demonstrating its implementation.
Syntax:
Given below is the syntax of the LIKE operator:
column name or expression LIKE pattern [ESCAPE character to be escaped]
Where column name or expression can be the name of the column of the particular table that you want to match with the pattern that is defined using regular and wildcard characters, we can even provide the character that is to be skipped while matching by specifying them in character to be escaped after the ESCAPE keyword which is again an optional thing. Mentioning the pattern with which we want to match the expression is required and must be specified. The LIKE operator can be used for matching in the query statements, including SELECT, INSERT, UPDATE, and DELETE statements.
Pattern Specification and Wildcard Usage
The pattern we have to match the expression can be a sequence of the regular and wildcard characters. The wildcard characters provide flexibility and variety in matching the expressions.
Regular characters are the string of alphabets and numbers that we want to search for, while wildcard characters can be one of the following:
- The wildcard with percentile signature (%) is used to specify that there can be one or more character occurrences over this place.
- The underscore character can be used to specify that there can be an occurrence of any of the single characters at the place where the single underscore wildcard character is specified (_).
- We can specify the list of the characters that can be allowed for a single occurrence at that place by mentioning them inside the square brackets [comma-separated list of allowed characters].
- We can even specify the range between which we can allow the single occurrence of the character within a specified range by mentioning the starting and ending character within range inside square brackets [starting character – ending character].
- Alternatively, we can also allow the presence of a single character that is not within the specified range by mentioning the range to be excluded between square brackets prefixing the range with ^ character [^].
Examples of SQL Wildcard Character
Given below are the examples of SQL Wildcard Character:
Let us consider the example by using a table named dictionary that contains the following records:
Code:
SELECT * FROM dictionary;
Output:
Now, we have to search for the records that contain the “word” string in their column values of meaning.
For this, we will use the following query containing the LIKE function.
Code:
SELECT * FROM dictionary WHERE meaning LIKE "%word%";
The execution of the above query statement gives the following output containing two records which have “word” in their meaning.
Output:
Using underscore (_) wildcard character to specify the single occurrence of any character between the specified strings or characters, we will consider one example where we will only get the records from the dictionary table that match the pattern that contains as many strings before and after the occurrence of I and I lying in between which can have any character in between the two I’s and specify _ underscore in between.
Our pattern will be “%i_i%”, and the query statement will be as follows:
Code:
SELECT * FROM `dictionary` WHERE meaning LIKE "%i_i%";
Output:
The output containing the above records were retrieved because of occurrence of words like “additional”, “origins”, “writing”, “similar” and “originality” in them that had only one character between two I characters and any of the words and characters before and after that pattern as specified by a % wildcard character.
Let us consider one more existing table named suppliers whose contents and structure are as shown in the query statement’s output.
Code:
SELECT * FROM Suppliers;
Output:
Now, we want to retrieve all the records of the suppliers whose name starts from the character A, G or T. For this, we will use the wildcard characters of square brackets and specify the allowed characters in the start position and further with the help of percentile wildcard character we will mention that any string or presence of any characters is allowed after the occurrence of one of the characters that are mentioned in our square brackets.
Hence, our pattern will be [AGT]% that includes wildcard characters, and our query statement will be:
Code:
SELECT * FROM Suppliers where SuppliersName like '[AGT]%';
Output:
Now, let us consider one more example that will demonstrate the usage and specification of the character that lies between the specified range of the alphabets. As discussed above, the range can be specified inside square brackets beginning and ending character of the range and a dash between. We want to retrieve all suppliers whose city names start within the range of D and H characters, followed by any characters.
Code:
SELECT * FROM Suppliers where city like '[D-H]%';
Output:
Conclusion
Many wildcard characters help us to define the patterns that will be used for matching the string values. The strings and column values and expressions can be matched with a particular pattern by using the LIKE operator in SQL that helps us specify the pattern that is the collection of different regular and wildcard characters. The use of wildcard characters makes the matching and pattern specification more flexible and easy.
Recommended Articles
We hope that this EDUCBA information on “SQL Wildcard Character” was beneficial to you. You can view EDUCBA’s recommended articles for more information.