How do we use LIKE in SOQL to find accounts that contain the string ABC? Using the following query

List<Account> accList = [SELECT Id FROM Account WHERE Name LIKE %ABC%];

Lets say, there is a set of Account Names with these 3 accounts below (abc, xyz, rst)

Set<String> setAccNames = new Set<String>();
setAccNames.add('ABC');
setAccNames.add('XYZ');
setAccNames.add('RST');

And we can use IN to get accounts within this set using the below query

List<Account> accList = [SELECT Id FROM Account WHERE Name IN :setAccNames];

Here comes the interesting part. If I want to check for Account Names that contain the strings (abc or xyz or rst) using LIKE but from with in this set of Account Names?

For this we need to set the Account Names in the set as below and then use the query.

setAccNames.add('%'+'ABC'+'%');
setAccNames.add('%'+'XYZ'+'%');
setAccNames.add('%'+'RST'+'%');

List<Account> accList = [SELECT Id FROM Account WHERE Name LIKE :setAccNames];
Advertisements