I'm building dynamic SOQL based on the User Input from UI.

I'm looking to build a wild card input search. For Example,

  1. if user enters "Apple*", query should return all records that start with "Apple" for that field.
  2. if user enters "*Apple*", query returns all records where field contains "Apple"

I've a dynamic query with LIKE '%Apple%' operator, but when "*" is entered, its treating it as value and no results returned. I want to know a way to process positioning of * and than dynamically create soql accordingly.


1 Answer 1


Hi this would just be a simple replacement in apex like so:

String s = '*Apple*'.replace('*', '%');
s = s;

Not the answer you're looking for? Browse other questions tagged .