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
augiewazaugiewaz 

LIKE Statement in SQL Not Working

Hi,
 
I am running the following sql statement in my Apex code. However, it is not working correctly.
The issue is that the SQL statement is not returning account records where the name of the account contains the term 'new'
However, if I was to just have a string (WHERE name LIKE '%new%'), it does retrieve account records where at least the word "new" appears in the account name.
 
String accName = 'New'
List<Account> acc = [SELECT id, name FROM Account WHERE name LIKE :accName]; // only returns accounts if the name matches 100%
List<Account> acc = [SELECT id, name FROM Account WHERE name LIKE '%new%']; // returns any accounts where the name contains the word 'new'
 
Any help on this is appreciated. From what I understand, how I have written the SQL statement is correct. Its just not doing what it is supposed to do.
 
Thanks
Warren
hisrinuhisrinu
Hi Warren,

 You can try this.


List<Account> acc = [SELECT id, name FROM Account WHERE name LIKE :accName+'%'];



Thanks
Srini
augiewazaugiewaz
Thanks Srini.
 
Problem solved!!