mysql Add text to fields before string that does not contain a specified string

All we need is an easy explanation of the problem, so here it is.

I know I can add text to all fields using this SQL query

update oc_product set sku= CONCAT('BBC', sku);

but I need it to only add if the field does not contain BBC in it.

As all SKU is added on auto and I am unable to change the way they are added to my DB so I need to do it manually every day.

thanks in advance for any help.

ie:

123456
bbc123456
1234567
bbc987654

I need to only add BBC to those where it is not listed using phpmyadmin.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Just use a where clause. If you are looking for 'BBC' anywhere in the string, then:

update oc_product
    set sku = CONCAT('BBC', sku)
    where sku not like '%BBC%';

If you are only looking for 'BBC' at the beginning of the string, then:

update oc_product
    set sku = CONCAT('BBC', sku)
    where sku not like 'BBC%';

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply