我的 SQL Server 表中有一些冗余的电子邮件地址,且同一名称出现在多行中。
我想在电子邮件中附加一个数字,以便他们的电子邮件变得不同。
我该怎么做?
最佳答案
您可以使用ROW_NUMBER函数来识别后续重复项,然后在前面添加 row_number,如下所示;
declare @t table (email varchar(100))
insert @t values ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="49392c3d2c09282b2a67263b2e" rel="noreferrer noopener nofollow">[email protected]</a>'), ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="593338373c1921202377362b3e" rel="noreferrer noopener nofollow">[email protected]</a>'), ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f791859293b783928483d994989a" rel="noreferrer noopener nofollow">[email protected]</a>'), ('<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7303160716331211105d1c0114" rel="noreferrer noopener nofollow">[email protected]</a>')
select
case
when row_number() over (partition by email order by email) > 1
then cast(row_number() over (partition by email order by email) as varchar(32)) + '_' + email
else email
end as email
from
@t
关于sql-server - 将多余的电子邮件替换为 SQL Server 中唯一的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29277331/