Ferenjito Ferenjito - 2 months ago 9
SQL Question

update field by extending original value in SQL

How do I update a field in SQL in a way that I keep the original value and just add a prefix or suffix to it?

update mytable set myfield = 'ABC'+myfield where id = 123


does not work.

Any ideas?

Answer

You have the right idea. Just use MySQL syntax:

update mytable
    set myfield = concat('ABC', myfield)
    where id = 123 ;

Note: If myfield could be NULL, then you might want:

update mytable
    set myfield = concat('ABC', coalesce(myfield, ''))
    where id = 123 ;