+ Start a Discussion
Vivekh88Vivekh88 

LIKE Operator in SOQL

Hi,

I have a soql query where i am using LIKE operator.
String sql = 'Select Name From Account WHERE Name LIKE \'%' + searchKeyword + '%\'';
Here 'searchKeyword' variable holds values like 'Test 1, Test 2, Test 3, Test 4'.
When i search a Name in my VF page which is a Textbox, like this 'Test 1, Test 4' its not returning the searchKeyword values. Because LIKE operator searches values only in Orderwise. It cant search middle values.

Is there any possible solution where it searches all values that are contained in the textbox?

Thanks
Vivek
Amit Chaudhary 8Amit Chaudhary 8
Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text strings and includes support for wildcards.
  • The % and _ wildcards are supported for the LIKE operator.
  • The % wildcard matches zero or more characters.
  • The _ wildcard matches exactly one character.
  • The text string in the specified value must be enclosed in single quotes.
  • The LIKE operator is supported for string fields only.
  • The LIKE operator performs a case-insensitive match, unlike the case-sensitive matching in SQL.
  • The LIKE operator in SOQL and SOSL supports escaping of special characters % or _.
  • Don’t use the backslash character in a search except to escape a special character.

For example, the following query matches Appleton, Apple, and Appl, but not Bappl:
SELECT AccountId, FirstName, lastname
FROM Contact
WHERE lastname LIKE 'appl%'

NOTE:- in like statement you can pass only single word or matching char not multiple string with ,

 
Vivekh88Vivekh88
Hi Amit,

Thanks for the reply. So you are telling we can search the values like 'Test 1, Test 2', Test 3. We cant search 'Test 1, Test 3'  Which is not in order?

Thanks
Vivek
Amit Chaudhary 8Amit Chaudhary 8
No. This is not related with order. If you want to search All Record Like 'Test 1, Test 2, Test 3' Then you just need to pass Test keyword.

Then your query will be like below
Select Name From Account WHERE Name LIKE '%Test%'

and above query will return all record which contain Test.
 
Vivekh88Vivekh88
Hi,
i am not passing 'Test' keyword in my query. I am using a apex variable. So that it accepts all values. So in that case i cant give 'Test' inside my query right?

Thanks
Vivek
Amit Chaudhary 8Amit Chaudhary 8
if you will pass searchKeyword = Test automaticaly your query will like above
rahul kumar 84rahul kumar 84
  String a= req.Params.get('AccName');
  String j= '%'+a+'%';
  List<Account> accList = [select id,Name,description from Account where name like :j];
Lucas CalegariLucas Calegari
Thank you Rahul!
Akhil MehraAkhil Mehra
Please Check this Out  
string searchquery='select name,id from account where name like \'%'+searchstring+'%\' Limit 20'; 
Lithium SalesforceLithium Salesforce
You need to have a variable with the search string the '%' concatenated and then pass that variable in the SOQL query:
String searchString = '%' + searchKeyword + '%';
List<Account> accountList = [Select Name From Account WHERE Name LIKE :searchString];