简体   繁体   English

MySQL:如何在字符串的开头搜索和替换字符

[英]MySQL: How do I search and replace chars at the beginning of a string

I'm trying to search and replace mobile numbers with the full international code. 我正在尝试使用完整的国际代码搜索和替换手机号码。 So where rows have 07970000007 to replace the beginning with +447970000007 所以行有07970000007以+447970000007替换开头

UPDATE tblMemberImportClub 
SET msisdn = REPLACE(msisdn, '07', '+447') 
WHERE INSTR(msisdn, '07') = 1;

But this also replaces the other matches: 但这也取代了其他比赛:

+4479700000+447 + 4479700000 + 447

I don't think i can use TRIM as some rows will already start with +447 and will therefore nor require any updates. 我不认为我可以使用TRIM,因为某些行已经以+447开头,因此也不需要任何更新。

Thanks in advance for any assistance. 在此先感谢您的任何帮助。

Use LIKE and INSERT() : 使用LIKEINSERT()

UPDATE tblMemberImportClub 
    SET msisdn = INSERT(msisdn, 1, 2, '+447') 
    WHERE msisdn LIKE '07%';

INSERT() is a string function that replaces exactly the characters you specify (see here ). INSERT()是一个字符串函数,它完全取代您指定的字符(请参见此处 )。

SELECT  

CONCAT(
                   REPLACE(
                             LEFT('07970000007',2), '07', '+447'
                   ),      
                   SUBSTRING('07970000007', 3, CHAR_LENGTH('07970000007'))
       )as replaced

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM