How to Use Wildcard Characters using Query function
Example
Let's assume we have the following data in a Google Sheet:
Name | Age | Location |
---|---|---|
Alice | 30 | New York |
Bob | 25 | Texas |
Carol | 35 | California |
David | 28 | Texas |
Eve | 40 | New York |
To use wildcard characters with the Query function in Google Sheets, follow these steps:
- Click on an empty cell where you want the results to appear, e.g., A7.
- Enter the following formula to search for names that start with "A" using a wildcard:
=QUERY(A1:C5, "SELECT * WHERE A LIKE 'A%'")
In this example, the wildcard character is %
, which matches any number of characters. The LIKE
keyword is used in the query to search for names that start with "A". The result will be:
Name | Age | Location |
---|---|---|
Alice | 30 | New York |
- If you want to search for names that contain "a" (case-insensitive) anywhere in the name, use the following formula:
=QUERY(A1:C5, "SELECT * WHERE lower(A) LIKE '%a%'")
In this case, we use the lower()
function to make the search case-insensitive. The result will be:
Name | Age | Location |
---|---|---|
Alice | 30 | New York |
Carol | 35 | California |
David | 28 | Texas |
- You can also use the
_
wildcard to match any single character. For example, to search for names that have exactly 4 characters and start with "D", use the following formula:
=QUERY(A1:C5, "SELECT * WHERE A LIKE 'D___'")
The result will be:
Name | Age | Location |
---|---|---|
David | 28 | Texas |
Remember to always enclose the search pattern in single quotes and use double quotes for the entire query string.
Did you find this useful?