0

Details:

I have stored physical paths into MySQL table. I have moved all content to a new folder.

This is the data in the database:

Current data---

g:\Folder1\File 1.jpg
g:\Folder1\Excel File.xlsx
g:\Folder1\Test.js

Desired change:

Here is what I'd like to achieve, add an extra folder to the path before the filename.

Desired---

g:\Folder1\New Folder\File 1.jpg
g:\Folder1\New Folder\Excel File.xlsx
g:\Folder1\New Folder\Test.js

Question

How can I achieve this? And just for future, how can I remove a specific folder from the path?

Eduards
  • 1,734
  • 2
  • 12
  • 37
  • Does this answer your question? [MySQL string replace](https://stackoverflow.com/questions/5956993/mysql-string-replace) – kmoser Jun 16 '20 at 12:59

2 Answers2

1

Simply use REPLACE as explained also in this SO question

UPDATE table
SET fied = REPLACE(field, 'g:\Folder1\', 'g:\Folder1\New Folder\')

It is untested so you may have to fix the \ escaping

Since REPLACE gives you the ability to replace the string with another one you can just modify the strings to "add or remove folders"

Lelio Faieta
  • 6,457
  • 7
  • 40
  • 74
0

Use replace function:

SELECT replace(filename, "Folder1\\", "Folder1\\New Folder\\")
FROM your_table;

You can specify a path from drive letter to avoid random data being replaced.

RusArtM
  • 1,116
  • 3
  • 15
  • 22