sql-server - 将多余的电子邮件替换为 SQL Server 中唯一的电子邮件

标签 sql-server

我的 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/

相关文章:

sql - SQL Server 中一行中所有列值的总和

sql - 返回 ID NOT IN 某表的结果

sql - Sql Server 中的交叉联接并不总是返回所有表中的所有行

sql-server - ROW_NUMBER 与 COUNT(1) 个?

sql-server - Node JS Sequelize v4 在将数据插入 SQL Server 时生成错误

sql-server - SSRS 报告生成器 : Prevent tablix moving horizontally?

SQL Server : update new record on insert

sql - 替代 MIN 选择多行

php - 无法使用 mssql_connect 从 php 连接到 mssql 服务器

sql - 更新 SQL Server 数据库表中的 2 个重复项之一