Search code examples
sqlmysql

mysql bulk rename, move a part of the string values from the beginning to the end of the string


example I have the following string values

'[123] X'
'[123] Y'
'[123] Z'
...

I want to bulk change them to

'X [123]'
'Y [123]'
'Z [123]'
...

X, Y, Z are completely random not sequential [123] is constant

I think the example above explains it all


Solution

  • I would use the REPLACE() function.

    REPLACE(str, find_string, replace_with)
    

    First remove the [123] from the string. Then add it again at the end of the string:

    UPDATE table_name SET column_name = CONCAT(REPLACE(column_name, '[123] ', ''), ' [123]');