Use REPLACE in update query using db_update I need to run an update query that involves using the SQL REPLACE and LEFT functions, and I need to do it with three different queries for multiple tables, so I'd like to do it in a loop. The problem I'm having is getting the REPLACE function to be interpreted properly. The query I need to run is this:

UPDATE field_data_field_myfield
SET field_myfield_value = 
REPLACE(field_myfield_value, 
'old_value', 'new_value');


From what I've found, I need to use the expression function like so:

foreach ($tables as $table =&gt; $field) {
  $num_updated_1 = db_update($table)
    -&gt;expression($field, 'replace(:field, :old, :new)', array(
      ':field' =&gt; $field,
      ':old' =&gt; 'old_value',
      ':new' =&gt;  'new_value',
    ))
    -&gt;execute();
}


However, when I run it like this, the field just gets updated with the field name as a string for every field.

What do I need to change for this to work?
The replace function doesn't work when the field is a placeholder. This will insert the field string as new value in the database. I've solved this problem removing the placeholder. Check it out:

foreach ($tables as $table =&gt; $field) {
  $num_updated_1 = db_update($table)
    -&gt;expression($field, 'replace(' . $field . ', :old, :new)', array(
      ':old' =&gt; 'old_value',
      ':new' =&gt;  'new_value',
    ))
    -&gt;execute();
}


This way the code will work perfectly. I hope this may help someone.