Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and replace string in MYSQL Database for a particular string only

I want to replace a particular string in mysql database, i am using this query :

UPDATE users SET name=replace(name,'raj','rajesh')

however what this query does is where it find raj it will replaces by rajesh for e.g if there is a string raju in databse after running this query raju becomes rajeshu which i dont want. i want a query which matches the replace string exactly means after running a query only 'raj' should get replaced with 'rajesh' and 'raju' should remain as is.. can someone please help??

like image 671
Ankit Avlani Avatar asked Dec 17 '14 06:12

Ankit Avlani


People also ask

How do I remove a specific character from a string in MySQL?

Remove characters from string using TRIM() TRIM() function is used to remove any character/ whitespace from the start/ end or both from a string.

How do I search a whole MySQL database for a particular string?

If you are using MySQL Workbench, right click on the database schema and select "Search Table Data..." then fill out the form. This will search the entire database.


3 Answers

Try below query to replace raj with rajesh

update users set name=replace(name,' raj ',' rajesh ');

OR

 update users set name=replace(name,'raj ','rajesh ') where name like '% raj %';
like image 183
Altmish-E-Azam Avatar answered Nov 01 '22 10:11

Altmish-E-Azam


Try this it will definitely work for you.

update users 
set name=replace(LOWER(name),'raj','rajesh') 
where 
name like 'raj %' 
OR 
name like '% raj %'
OR
name = 'raj'
like image 36
codemaniac Avatar answered Nov 01 '22 09:11

codemaniac


This query works for me:

UPDATE users 
SET name = replace(name,'raj','rajesh')
WHERE name = 'raj'
like image 38
Ankit Avlani Avatar answered Nov 01 '22 09:11

Ankit Avlani