25

I have a MySQL database table 'photos' with a column 'filename'. I need to replace the spaces in the filename column values with underscores. Is it possible with a single/multiple query? If so how?

Mithun Sreedharan
  • 49,883
  • 70
  • 181
  • 236

2 Answers2

64

You can use the REPLACE function :

REPLACE(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str.
REPLACE() performs a case-sensitive match when searching for from_str.

So, to replace all occurences of a character by another one in all lines of a table, something like this should do :

update photos set filename = replace(filename, ' ', '_');

ie, you search for ' ' in the column filename and use '_' instead ; and put the result back into filename.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
6
update photos set filename = replace(filename,' ', '_');
Ghostman
  • 6,042
  • 9
  • 34
  • 53
Don
  • 4,583
  • 1
  • 26
  • 33