Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL string replace

Tags:

replace

mysql

I have a column containing urls (id, url):

http://www.example.com/articles/updates/43 http://www.example.com/articles/updates/866 http://www.example.com/articles/updates/323 http://www.example.com/articles/updates/seo-url http://www.example.com/articles/updates/4?something=test 

I'd like to change the word "updates" to "news". Is it possible to do this with a script?

like image 738
n00b Avatar asked May 10 '11 21:05

n00b


People also ask

How do you replace something in MySQL?

MySQL REPLACE() FunctionThe REPLACE() function replaces all occurrences of a substring within a string, with a new substring. Note: This function performs a case-sensitive replacement.

How do I change the first character in MySQL?

You could simply use: UPDATE customers_basket SET products_id=CONCAT('S', SUBSTRING(products_id FROM 2)); i.e.: Instead of replacing the initial "U" with an "S", simply start with an "S" and copy the remaining characters.


1 Answers

UPDATE your_table SET your_field = REPLACE(your_field, 'articles/updates/', 'articles/news/') WHERE your_field LIKE '%articles/updates/%' 

Now rows that were like

http://www.example.com/articles/updates/43

will be

http://www.example.com/articles/news/43

http://www.electrictoolbox.com/mysql-find-replace-text/

like image 62
Giraldi Avatar answered Sep 21 '22 18:09

Giraldi