SQL,用电子邮件替换字符串的一部分

时间:2018-10-04 05:48:09

标签: mysql sql

在我的数据库中,我有一列user_email,其值是:

aaa@test.com
bbb@test.com
ccc@test.com

我只想更改@后面的部分电子邮件地址,以使结果列具有以下值:

aaa@other.net
bbb@other.net
ccc@other.net

我该如何实现?

5 个答案:

答案 0 :(得分:1)

我发现以下解决方案似乎可以解决问题:

UPDATE table_name SET user_email = REPLACE(user_email, '@test.com', '@other.net');

答案 1 :(得分:0)

使用SUBSTRING_INDEXconcat功能

select concat(SUBSTRING_INDEX("aaa@test.com", "@", 1),'@other.net')

输出aaa@other.net

因此您的列user_email将会是

select concat(SUBSTRING_INDEX(user_email, "@", 1),'@other.net')

答案 2 :(得分:0)

使用替换功能

demo

select replace(name,substring(name,position('@' in name),length(name)-position('@' in name)+1),'@other.net')

select replace('aaa@test.com',substring('aaa@test.com',position('@' in 'aaa@test.com'),
length('aaa@test.com')-position('@' in 'aaa@test.com')+1),'@other.net')

输出:

val             n
aaa@test.com    aaa@other.net

答案 3 :(得分:0)

Cannot interpret token '!' at position 5.

View on DB Fiddle

答案 4 :(得分:0)

您可以将replacesubstrinstr一起使用:

SELECT replace( 'aaa@test.com',
         substr('aaa@test.com',instr('aaa@test.com','@'),length('aaa@test.com'))
                              ,'@other.net') as result_str;

 result_str
-------------
aaa@other.net

或在您的表(tab)中带有称为email的列:

select replace(email,substr(email,instr(email,'@'),length(email)),'@other.net') result_str
  from tab;

result_str
-------------
aaa@other.net
bbb@other.net
ccc@other.net

Rextester Demo