Queries
SOQL LIKE
Pattern Matching
SOQL LIKE matches patterns with % and _ wildcards.
Introduction to SOQL LIKE
The SOQL LIKE operator is used for pattern matching in Salesforce Object Query Language. It is particularly useful when you need to search for records where a field matches a specified pattern using wildcards.
Understanding Wildcards with LIKE
SOQL LIKE supports two types of wildcards:
- % - Matches zero or more characters.
- _ - Matches exactly one character.
These wildcards allow you to perform more flexible searches compared to exact string matching.
Using the % Wildcard
The % wildcard in SOQL allows you to match any sequence of characters. This can be useful when you want to find records containing a specific substring within a field.
In this example, the query retrieves all accounts whose names contain the substring "Tech" anywhere within the name.
Using the _ Wildcard
The _ wildcard is used to match a single character. This can be helpful when you need to find records with a specific character pattern.
This query selects accounts where the Name field starts with the letter 'A', is followed by any single character, and then followed by 'm'.
Combining Wildcards
You can combine both % and _ wildcards in a single LIKE statement to create complex patterns.
Here, the query retrieves accounts with names starting with 'A', followed by any single character, then any character sequence, and finally containing 'Tech'.
Case Sensitivity in SOQL LIKE
SOQL LIKE is case-insensitive by default. This means that patterns like 'tech', 'Tech', and 'TECH' will all match the same records.
Practical Considerations
When using the LIKE operator, consider the performance implications, especially on large datasets. Wildcard searches can be resource-intensive, so it's important to optimize your queries as much as possible.