How to Query SOQL NOT LIKE Operator in Salesforce

In Salesforce, SOQL supports various operators for applying filters and conditions to query data, and one such operator is NOT LIKE. This operator is helpful in SOQL queries when you need to exclude records that match a specific pattern.

In this Salesforce tutorial, I will explain how to use the NOT LIKE operator in Salesforce, and with the help of examples, we will discuss the use case of the NOT LIKE operator in SOQL queries.

NOT LIKE Operator in Salesforce SOQL

In Salesforce SOQL, the NOT LIKE operator filters records that do not match a specified pattern. It works with text fields and uses wildcard characters such as ‘%’ and ‘_’ to define patterns.

Where ‘%’ is used to match zero or more characters, and underscore ‘_’ is used to match a single character.

The syntax for using the NOT LIKE operator in SOQL :

SELECT fields
FROM ObjectName
WHERE NOT FieldName LIKE 'text value'

Now, we will see the following scenarios for using the NOT LIKE operator in Salesforce SOQL queries.

Query to Exclude Records Containing Specific Text

For example, you have to query account records and want to exclude the documents where the name does not contain ‘Chemicals,’ so we can use the NOT LIKE operator to exclude those records.

SOQL query using NOT LIKE:

SELECT Id, Name
FROM Account
WHERE NOT NAME LIKE '%Chemicals%'

The above SOQL query will return the account name records, excluding the one with the text value “Chemicals.

Output:

SOQL NOT LIKE Operator in Salesforce

This way, we can use the NOT LIKE operator in the Salesforce SOQL query to exclude records containing specific text values.

SOQL NOT LIKE Operator to Exclude Records Starting with Specific Characters

Now, instead of using a text, when you need to exclude text with specific characters, such as accounts whose names begin with “Uni,” we can use the NOT LIKE operator.

SOQL query with NOT LIKE operator:

SELECT Id, Name 
FROM Account 
WHERE NOT Name LIKE 'UNI%'

Output:

How to use NOT like in Salesforce SOQL

SOQL NOT LIKE operator to exclude records ending with specific characters

The way we excluded the records with specific characters using the NOT LIKE operator in the previous example, we can also remove records that end with specific characters.

For example, I have executed an SOQL query that will exclude the account records with the character ‘s‘ in the end.

SELECT Id, Name 
FROM Account 
WHERE NOT Name LIKE '%s'

Output:

Exclude records with specific characters in Salesforce SOQL

In the output, the query returned the account names and excluded the names ending with ‘s.’

SOQL NOT LIKE Operator to Exclude Specific Substring Matches

A specific substring can be a value like “2025-Q1“. For example, we have this value in the opportunity names, so we use the syntax below to remove the records with this value.

SELECT Id, Name 
FROM Opportunity 
WHERE  NOT NAME LIKE '%2023-Q1%'

This query filters out opportunities containing “2025-Q1” anywhere in their name.

Add Multiple Filters with NOT LIKE in SOQL

When you have to query multiple other operators and values using NOT LIKE, then you can follow the query below.

For example, we need to query the account records, excluding those with the industry as “technology” and those with the text “chemicals” anywhere in the name. For this, we can use the query below.

SELECT Id, Name, Industry FROM Account WHERE NOT (Industry LIKE '%Technology%' AND Name LIKE '%Chemicals%')
Add multiple filter with NOT LIKE in Salesforce SOQL

This way, we can add multiple filters to the NOT LIKE operator in Salesforce SOQL.

Conclusion

In Salesforce SOQL, the NOT LIKE operator is useful for filtering out records that match specific patterns and excluding them from query results.

In this Salesforce SOQL tutorial, we have learned about the NOT LIKE operator and also its execution in different scenarios.

Following the above syntax, you can efficiently execute the NOT LIKE operator in your SOQL queries to exclude specific records.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.