Play Games

Search This Blog

Friday, March 24, 2017

Using Like in SOQL Query

Example 1: To find all the accounts which contain "s" in their names using LIKE in SOQL Query:
Sample Code:
String querySrt = 'Select id,name from Account where name !=null and name Like  \'%s%\'';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);

Example 2: To find all the accounts which contain "s" in their names using LIKE in SOQL Query:
Sample Code:
String querySrt = 'Select id,name from Account where name !=null and name Like  \'%b%\'';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);


Example 3: To find all the accounts which contain "s" OR "Q" OR "M" in their names using LIKE in SOQL Query:
Sample Code:
Set<String> setAccountNames = new Set<String>();
String querySrt = 'Select id,name from Account where name !=null ';
setAccountNames.add('%'+'m'+'%');
setAccountNames.add('%'+'s'+'%');
setAccountNames.add('%'+'q'+'%');
querySrt = querySrt + ' and name Like :setAccountNames';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);

No comments:

Post a Comment