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
Leticia Monteiro Freitas 4Leticia Monteiro Freitas 4 

Select like with a variable

Hello, 

I'm have an apex class that recieves a variable name and then has to do an query with thin name on Accoun object.
But my variable could use some part of this name only, like when I query the name Liza, I should return every account that has Liza in the middle of it, like Elizabeth.

Can anyone help me?
I'm trying use the operator like, but i dont know how use the syntax for a variable.
 
Best Answer chosen by Leticia Monteiro Freitas 4
Raj VakatiRaj Vakati
Try this 
 
list<Acccount> lstaccs = [select id,name from Acccountwhere 
                        name like : '%'+'LISA'+'%';];



OR

 
string names = '%'+name+'%';
list<account> lstaccs = [select id,name from account where 
                        name like : names];

 

All Answers

v varaprasadv varaprasad
Hi Leticia,

Try This : 
 
string names = '%'+name+'%';
list<account> lstaccs = [select id,name from account where 
                        name like : names];
system.debug('==lstaccs=='+lstaccs);

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1




 
Raj VakatiRaj Vakati
Try this 
 
list<Acccount> lstaccs = [select id,name from Acccountwhere 
                        name like : '%'+'LISA'+'%';];



OR

 
string names = '%'+name+'%';
list<account> lstaccs = [select id,name from account where 
                        name like : names];

 
This was selected as the best answer