Skip to main content
New tool CRON Expression Builder — preview next run times before you schedule Apex. Open the builder →
Diagram illustrating the syntax and usage of the LIKE keyword within a standard SOQL query structure.
Apex

SOQL LIKE & NOT LIKE: Wildcards, Examples & Best Practices

SOQL LIKE and NOT LIKE explained: wildcards (% and _), case sensitivity, escaping, performance gotchas, and the exact Apex bind-variable patterns developers actually use.

Applications never be static, they needs to have dynamic functionality. Getting the input from end user, retrieving the data from database, processing on it and then showing the result on user scree. This is what expected from an application. In Salesforce, SOQL language is used to retrieve the data from database. You may face a scenario to retrieve the object records if a particular field value is having 'XYZ' as text. This is where the LIKE keyword is used in SOQL. In this topic, we will try to cover almost all possible scenarios with LIKE keyword.

Scenarios with SOQL LIKE operator

Find the list of account which is having word "UC" in its name.

This is the pretty straightforward scenario we can say, to find such accounts, we need to write SOQL query as follows:

List<Account> Accounts = [SELECT Id, Name from Account where Name like '%UC%']

Lets move towards some advanced scenarios.

Find the list of account which is having the word entered by user on form

This is something we can call dynamic query, because we need to find the accounts based on the word entered by user on UI.

Lets say you have declared and using the apex string variable "SearchKeyword" on visualforce or lightning component. When user enters the keyword and click on submit button, this variable will be holding the value of keyword.

To query all such accounts which is having the keyword in its name, we ofcourse need to user like query. But does the following query will work?

List<Account> Accounts = [SELECT Id, Name from Account where Name like: SearchKeyword]

No its not, you have to append "%" from both sides. To do that, following needs to be done in apex:

SearchKeyword = '%'+SearchKeyword+'%';
List<Account> Accounts = [SELECT Id, Name from Account where Name like : SearchKeyword];

Find the list of account in which name contains either of the words

Suppose we have a list of search keywords in apex as follow:

List<String> Keywords = new List<String>{'Keyword 1', 'Keyword 2', 'Keyword 3'};

Now to find the list of accounts in which name contains any of the text present in the above list, we need to write SOQL query as:

List<Account> Accounts = [SELECT Id, Name from Account where Name like : Keywords ];

Query will automatically compile even if its a single string or the list of string.

We have seen scenarios which gives results of having a keyword, but what if we need the results which do not have the keywords. In this case with need to use NOT along with LIKE operator in SOQL.

Find the list of account which is NOT having word "UC" in its name.

Its the basic query and will look like:

List<Account> Accounts = [SELECT Id, Name from Account where (NOT Name like '%UC')

Find the list of account where name do not contain word entered by user

The query will become:

SearchKeyword = '%'+SearchKeyword+'%';
List<Account> Accounts = [SELECT Id, Name from Account where (NOT Name like : SearchKeyword)];

Placement of "%" character also plays important role to give you the results based upon the need. There are following scenarios with % character

  • Starting with text 'ABC' : 'ABC%'
  • Ending with text 'ABC' : '%ABC'
  • Contains text 'ABC' : '%ABC%'
  • Starting with 'ABC' and Ending with 'PQR' : 'ABC%PQR'

Frequently asked questions

What is the SOQL LIKE operator?

The SOQL LIKE operator performs partial-string matching against text fields, using % as a multi-character wildcard and _ as a single-character wildcard. Example: WHERE Name LIKE '%UC%' returns every record whose Name contains the substring UC. LIKE is case-insensitive by default and works on String, ID, Picklist, and Phone fields.

How do I use NOT LIKE in SOQL?

Wrap the LIKE clause in parentheses with NOT in front: SELECT Id, Name FROM Account WHERE (NOT Name LIKE '%UC%'). The NOT keyword cannot be applied directly to LIKE without parentheses — that's the most common syntax error developers hit. NOT LIKE returns every row that doesn't match the pattern, including rows where the field is null.

Is SOQL LIKE case-sensitive?

No. SOQL LIKE is always case-insensitive — there is no case-sensitive variant. WHERE Name LIKE 'apple' matches Apple, APPLE, and aPpLe. If you need case-sensitive matching, you must filter the results in Apex after the query.

What wildcards does SOQL LIKE support?

Two: % matches zero or more characters, and _ (underscore) matches exactly one character. So 'AB_C%' matches AB1Cany, AB9Czero, etc. There is no equivalent of regex character classes — for richer patterns, do post-query filtering in Apex or use SOSL FIND.

How do I escape special characters in a SOQL LIKE query?

Escape the wildcard characters % and _ with a backslash: WHERE Name LIKE '50\% off' matches the literal string '50% off'. Single quotes inside Apex string literals must be escaped with a backslash too: '\'Acme\''. When binding via Apex variables, use String.escapeSingleQuotes(input) to prevent SOQL injection.

Does SOQL LIKE work on Picklist or ID fields?

Yes. LIKE works on String, ID, Picklist (single-select), Phone, Email, URL, and TextArea fields. It does NOT work on Long Text Area, Rich Text, Number, Date, Boolean, or Multi-Select Picklist fields. For free-text search across multiple fields, use SOSL FIND instead — it's more efficient than chaining many LIKE predicates.

Why is my SOQL LIKE query slow?

A LIKE pattern that starts with % (e.g., '%UC%') cannot use a standard index — Salesforce has to do a full table scan. To make it indexable, anchor the pattern at the start (e.g., 'UC%') or contact Salesforce Support to enable a custom index. For LDV (large data volumes), prefer SOSL or rewrite the query with a more selective filter.

Newsletter

One email every Tuesday

New guides, tool updates, and the release-note changes that break things.

No spam. Unsubscribe in one click.

Comments

Loading comments...

Leave a Comment