0

I have a MySQL database with about 20.000 entries where files are named Name_Subname_XXXXX. Because the files are named that way, the name that shows to the internet itself also get's that name when entered into the database.

I wonder how can I in an easy way remove the _ from the name and just keep Name Subname XXXXX?

potashin
  • 44,205
  • 11
  • 83
  • 107
Rizzzler
  • 193
  • 2
  • 12
  • $newfilename = str_replace('_',' ',$filename); – Junaid Ahmed Jun 17 '15 at 11:49
  • 1
    possible duplicate of [How can I use mySQL replace() to replace strings in multiple records?](http://stackoverflow.com/questions/4271186/how-can-i-use-mysql-replace-to-replace-strings-in-multiple-records) – Alex K. Jun 17 '15 at 11:50

4 Answers4

0

You can use replace:

select replace(col, '_', ' ') from tbl
potashin
  • 44,205
  • 11
  • 83
  • 107
0

You can use MySQL's REPLACE method for strings:

UPDATE tbl SET filename = REPLACE(filename, '_', ' ');
wonderb0lt
  • 2,035
  • 1
  • 23
  • 37
0

use replace()

SELECT REPLACE(col, '_', ' ') from table;

Ref https://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_replace

Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
0

I am guessing that you probably want to split the name into three columns (as opposed to just removing an underscore in a name). If so, use substring_index():

select substring_index(filename, '_', 1) as name,
       substring_index(substring_index(filename, '_', 2), '_', -1) as subname,
       substring_index(filename, '_', -1) as xxxxx
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786