How to use conditions on a REPLACE expression? For an autocomplete, I'm searching a table of address (full address in one column).

Example: 123 Example building, Somestreet, Town, QW1E 2RT

To improve the search I want to remove the space, so you can search QW1E2RT or QW1E 2RT, however, I obviously don't want that to be displayed to the user.

I think I'm most of the way there:

$query = \Drupal::database()-&gt;select('mctable', 'mc');
$query-&gt;addField('mc', 'a_address');
$query-&gt;where('REPLACE(mc.a_address, \' \', \'\') LIKE \':value\'', [':value' =&gt; '%' . $string . '%']);
$query-&gt;distinct();
$rows = $query-&gt;execute();


The problem is that the :value placeholder doesn't appear to be passed through… If I hardcode a value in the "snippet" it works.

I originally tried with:

$query-&gt;addExpression('REPLACE(mc.a_address, \' \', \'\')', 'a_address');
$query-&gt;condition('a_address','%' . $query-&gt;escapeLike($string) . '%', 'LIKE');


But couldn't figure out how to use the expression in the condition.

Many Thanks for reading and helping.
The problem with the code above is the apostrophes around the placeholder.

LIKE \':value\'', should be LIKE :value',.

I would still like to know how to do this with addExpression though :-/