Stuff…

I know I will forget.

Stuff… header image 2

MySQL replace() function

January 8th, 2009 · 1 Comment

URL:

update [table_name] set [field_name] = replace([field_name],’[string_to_find]‘,’[string_to_replace]‘);

Tags: MySQL

1 response so far ↓

  • 1 jonathan // Feb 24, 2011 at 10:13 pm

    another good one is

    concat()

    eg

    update [tablename] set [field_name1] = concat([field_name1],[fieldname2])

    useful if you have to make keywords or descriptions from other fields

    also useful is trim()
    eg

    select trim(trailing ‘.png’ from [field_name]) will get the image name without the .png on the end too, so you can do

    update [tablename] set [field_name2] = trim(trailing ‘.png’ from [table_name])

    very useful if importing images somewhere and you have 3 cols of images, small, med and large, etc and all have the same name, just with an _sm etc on the end

    that one would be

    update [tablename] set [field_name2] = concat(trim(trailing ‘.png’ from [table_name]),’_sm.png’)

    then you end up making

    image_sm.png in field2 from image.png in field 1

Leave a Comment