Remove pattern from database
I have a MySQL table with over 3 million rows that includes peoples names and the state they are from e.g. "Smith (SC)" I would like to remove the state info a开发者_如何学运维nd have "Smith". Since there are many difference last names and states used I was wondering if there was any trick to make this easier and just remove the parentheses and the text contained inside of them.
Any advice would be greatly appreciated.
Test with a select to make sure you're getting good results:
SELECT LastName, LEFT(LastName, LOCATE('(', LastName) - 1) AS CorrectedLastName
FROM YourTable
WHERE LOCATE('(', LastName) <> 0
Once you're confident in the results:
UPDATE YourTable
SET LastName = LEFT(LastName, LOCATE('(', LastName) - 1)
WHERE LOCATE('(', LastName) <> 0
Make a backup first, then try
Update <tablename>
set name =substr(name,1, instr(name,'('));
substr is for oracle but check the mysql equivalent
精彩评论