Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
podcast
Filter by Categories
ArcGIS Pro
GDAL
GeoJson
Map
Python
QGIS
Uncategorized

Using The “LIKE” Operator In QGIS

How to use The “LIKE” Operator In QGIS

In QGIS, the LIKE operator is used in expressions to match a pattern in a string. This operator is often used in queries or expressions for filtering data.

Here’s how to use the LIKE operator in QGIS:

  1. Open the Attribute Table: First, open the attribute table of the layer you want to query.
  2. Open the Query Builder:
  • You can filter features by opening the Query Builder. Right-click on the layer in the ‘Layers’ panel, select Filter... from the context menu, or click on the Select features using an expression button in the attribute table toolbar to open the expression builder if you want to select specific features based on the query.
  1. Using the LIKE Operator:
  • In the Query or Expression Builder, you can write an expression using the LIKE operator. Here is a basic syntax:
    sql "fieldname" LIKE 'pattern'
  • For example, if you have a field named city and you want to find records where the city name starts with ‘New’, you would use:
    sql "city" LIKE 'New%'
  • The % symbol is a wildcard that matches zero or more characters, so ‘New%’ will match any string that starts with ‘New’.
  1. Case Sensitivity:
  • By default, LIKE is case-insensitive in most setups, but this can depend on the data source and QGIS settings. If you need case-sensitive matching, you can use ILIKE for case-insensitive queries.
  1. Using Wildcards:
  • %: Matches any sequence of characters (including none).
  • _: Matches exactly one character.
  1. Examples:
  • Find any names that end with “son”:
    sql "name" LIKE '%son'
  • Find any names that contain “berg” anywhere:
    sql "name" LIKE '%berg%'
  • Exact matches can be done without wildcards:
    sql "name" LIKE 'James'

Once you’ve written your query using the LIKE operator, you can run it to filter or select the data. This is a powerful way to work with pattern matching in QGIS, especially for text data fields.

Frequently asked questions about using the LIKE operator in QGIS:

1. How do I use wildcards with the LIKE operator in QGIS?

Wildcards are special characters that allow you to create flexible search patterns in string fields. In QGIS, the % wildcard matches any sequence of characters, while _ matches any single character. For example:

  • To find all entries that start with “Lake”, you would use: "name" LIKE 'Lake%'
  • To find all entries that have exactly three letters followed by “ton”, you would use: "name" LIKE '___ton'

2. Is the LIKE operator case-sensitive?

In QGIS, the LIKE operator is generally case-insensitive when used with most databases. This means that "name" LIKE 'john%' will match “John”, “john”, “JOHN”, etc. If you need a case-sensitive match, you should check the specifics of your database management system or use SQL functions to enforce case sensitivity. Alternatively, ILIKE can be used for explicitly case-insensitive queries.

3. Can I use the LIKE operator to filter numeric fields?

LIKE is intended for text fields. To use it on numeric fields, you must first convert the numeric values to text. In QGIS, you can do this with the to_string() function:

  • "population" LIKE '1234%' would become to_string("population") LIKE '1234%'

4. How do I escape special characters in LIKE queries in QGIS?

To include literal instances of % or _ in your LIKE queries, you need to escape them using the backslash \. For example:

  • To find entries that include “100%” you would use: "description" LIKE '100\%'
  • The exact escape character might vary depending on the database backend.

5. Can I use the LIKE operator in the Field Calculator?

Yes, the LIKE operator can be used in the Field Calculator to create new columns based on conditional checks or to update existing fields. For example, you could set up a new field to indicate whether a name field contains a certain substring:

  • "name" LIKE '%Smith%' could be used to populate a new boolean field with True for names containing “Smith”.

6. How can I combine multiple LIKE conditions?

You can combine multiple LIKE conditions using AND and OR in your queries to create more complex filters. For example:

  • To find names that start with “J” and end with “n”: "name" LIKE 'J%' AND "name" LIKE '%n'
  • To find names that start with “Al” or “Bob”: "name" LIKE 'Al%' OR "name" LIKE 'Bob%'

7. What are the performance implications of using LIKE?

Using the LIKE operator, especially with a leading wildcard (e.g., %name), can be slow because it often prevents the database from using indexes efficiently. If performance is an issue, consider using full-text search capabilities if available, or ensure that the wildcards are not leading the search pattern whenever possible.

8. How does the LIKE operator differ from REGEXP in QGIS?

LIKE provides simple pattern matching with limited wildcard characters. REGEXP, on the other hand, allows for much more complex pattern matching using regular expressions. REGEXP is more powerful but also more complex to write and potentially slower. For example, REGEXP can match patterns based on character classes, repetitions, and more sophisticated constructs than LIKE can handle.

About the Author
I'm Daniel O'Donohue, the voice and creator behind The MapScaping Podcast ( A podcast for the geospatial community ). With a professional background as a geospatial specialist, I've spent years harnessing the power of spatial to unravel the complexities of our world, one layer at a time.