Ruchika Ruchika - 1 year ago 79
MySQL Question

Concatenate text in Field Value in Database where Fieldvalue is not empty

I want to concatenate specific text before existing text in database column, where field value is not empty

For instance - i have column v_small_img - where there are many fields which are blank and some have values

I need to append in text only in fields which have values and not to append text in fields which are blank

I have reached in to this,

UPDATE `#_toys_variants` SET v_small_img=CONCAT('images/uploads/variants/',v_small_img);

but the code can actually include in 'images/uploads/variants/' in all fields, instead of checking which fields are empty

How to achieve the desired

Answer Source

You can filter the non empty v_small_img in WHERE clause:

UPDATE `#_toys_variants`
SET v_small_img = CONCAT(
WHERE v_small_img <> ''


In order to rollback the empty value in v_small_img field which might hold this value 'images/uploads/variants/' because of your above query :

UPDATE `#_toys_variants`
SET v_small_img = ''
WHERE v_small_img = 'images/uploads/variants/'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download