function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shri kshri k 

Soql to search Accounts containing (_) underscore

I want to search Accounts which contains underscores, suppose I have accounts with names test,test_123,test_test,test_456 and when i fire following query it is returning me 4 rows where it should return 3only.

 

Query :

   select id from account where name like  '%test_%'

 

Salesforce considers ( _ ) as any single charatcter and thats why it is returning me 4 rows is there any way to overcome this. I have tried using escape sequence '%test\_%' but it is also not working.

 

Any pointers??

Best Answer chosen by Admin (Salesforce Developers) 
Manos SpanoudakisManos Spanoudakis

so then you need to escape the \ and do it like this 

 

String searchString = '%test\\_%';

 

Worked for me it only returns Accounts Test_1 and Test_2

 

while the 

 

String searchString = '%test_%';

version

also returns accounts like "Test Account" 

 

 

 

All Answers

Manos SpanoudakisManos Spanoudakis
try double \ -> \\
shri kshri k

Not working with \\ too :(

Manos SpanoudakisManos Spanoudakis

SELECT Id,Name FROM Account WHERE Name LIKE '%test\_%' works fine for me !!

shri kshri k

Yup that works fine!!

but consider the fillowing case

String searchString = '%test\_%';

List<Account> lstAcc = [select id from Account where Name like : searchString];

or

you can execute your own query in developer console not in Query editior it works in Query editor or force.com explorer but in the step i writtent above, this is not valid sequence for Salesforce

Manos SpanoudakisManos Spanoudakis

so then you need to escape the \ and do it like this 

 

String searchString = '%test\\_%';

 

Worked for me it only returns Accounts Test_1 and Test_2

 

while the 

 

String searchString = '%test_%';

version

also returns accounts like "Test Account" 

 

 

 

This was selected as the best answer
shri kshri k
wow this worked gr8!!