Is it posible to do something like:

db_update('my_module_table')->fields( array('myfield' => <myfield> + 'newstring') );

Or do I have to load the field value first by using db_select() ?

Maybe someway with db_query() ?

  • you should do db_update('my_module_table')->fields( array('myfield' => 'myfield'.'newstring')); – AjitS Sep 18 '12 at 14:58
  • @indrock this will not work. – Mathankumar Sep 18 '12 at 15:08

You can still use db_update() just with an expression() instead of fields(), e.g.

  ->expression('myfield', 'myfield + :newstring', array(':newstring' => 'Some string'))

Although it strikes me that still wouldn't work as you're trying to concatenate strings (the + operator doesn't perform string concatenation as far as I know). This should do it:

  ->expression('myfield', 'CONCAT(myfield, :newstring)', array(':newstring' => 'Some string'))
| improve this answer | |
  • this looks like an answer. – AjitS Sep 18 '12 at 15:11
  • It does I'm trying this now... – FLY Sep 18 '12 at 15:17
  • works but not on an empty database field when it is run for the first time. – FLY Sep 18 '12 at 15:31
  • 4
    Hmm it should do (unless the field defaults to NULL, in which case I guess it wouldn't). You could get a bit more inventive: ->expression('myfield', 'IF(myfield IS NULL, :newstring, CONCAT(myfield, :newstring)), array(':newstring' => 'Some string'));...but I haven't tried that – Clive Sep 18 '12 at 15:34
  • Yes field defaults to NULL. I'll figure it out with your last tip thanks! – FLY Sep 18 '12 at 15:36

You can use the following,

    ->expression('field', "concat(field, 'new String')")
| improve this answer | |
  • Any reason? Its a working example. – Mathankumar Sep 18 '12 at 15:14
  • 1
    yes this works but not on an empty database field when it is run for the first time. I like @clive answer better and he was first as well. – FLY Sep 18 '12 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.