在SQL中更新电子邮件字段值

时间:2015-05-27 13:15:21

标签: sql sql-server-2008

我的表email中有一列person,电子邮件列中包含

等条目
sam@yahoo.com
xxxashish@gmail.com
sunny@rediff.com
xxxSomil@google.com

现在,我想要更新电子邮件列的值,并从电子邮件ID的开头删除所有xxx。任何想法如何实现这一目标?

示例xxxashish@gmail.com应为ashish@gmail.com

3 个答案:

答案 0 :(得分:6)

UPDATE person SET email = RIGHT(email, LEN(email) - 3) WHERE email LIKE 'xxx%';

这样你就不会取代文本中可能出现的xxx

因此xxxladidaxxx@gmail.com将返回ladidaxxx@gmail.com

答案 1 :(得分:4)

update 
person
set email = REPLACE(email,'xxx','')
where email like 'xxx%'

如果xxx介于电子邮件之间,上面的内容也将替换它。对于以下代码,

   update 
    person
    set email = RIGHT(email, LEN(email) - 3) 
    where email like 'xxx%'

答案 2 :(得分:0)

您也可以查看。

Declare @t table(email varchar(50))

insert into @t values ('sam@yahoo.com'), ('xxxashish@gmail.com' ),('sunny@rediff.com' ),('xxxSomil@google.com')

UPDATE @t
SET email = STUFF( email , 1 , 3 ,'')
WHERE email LIKE 'xxx%'

select * from @t

How to remove the first character if it is a specific character in SQL