开发者

Remove pattern from database

开发者 https://www.devze.com 2023-04-07 18:29 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号