如何从 T-SQL 中的一串电子邮件中查找和删除电子邮件令牌?

How do I find and remove an email token from a string of emails in T-SQL?

我正在尝试标准化字段中的电子邮件地址。以下是我试图解决的问题的几个示例:

ex 1 yyyyyyyy.xxxxxxxxx.p4elr874@carbuyingemail.com;yyyyyyyy.xxxxxxxxx.daojffab@carbuyingemail.com;xxxxxxxxx@aol.com
ex 2 xxxxxx51@gmail.com;yyyyyyyyy.xxxxxx.t78m45be@truecarcustomer.com;yyyyyyyyy.xxxxxx.dn4m9ujo@truecarcustomer.com
ex 3 3xxxxx996@anon.cargurus.com;xxxxxxxxxxx@comcast.net;3xx68xxxx@anon.cargurus.com;4xx2x3xxx@anon.cargurus.com

` 我正在尝试从与此类似的字符串中的自动生成的电子邮件中提取可用的电子邮件,例如

(ex 1) 我想要@aol.com 地址;
在 (ex 2) 中,我想要 @gmail.com 地址;
在 (ex 3) 中,我想要 @comcast.net 地址。

我尝试使用 case 语句查找最流行的服务提供商的 charindex,但由于我不知道 @ 符号之前存在多少个字符,所以我不知道如何使用子字符串方法。

一个字符串中可以包含 2 到 7 封电子邮件。我知道如何为 2 做这件事。那很容易,但在那之后我就不知道了。这是我目前所拥有的:

select email, len(email) - len(replace(email,';','')) num_semi
into #tmp1
from staging_transformations
where email != ''  
order by len(email) - len(replace(email,';','')) desc


select email,
case
    when num_semi = 1 then
        case
            when SUBSTRING(email,0,charindex(';',email)) like '%@anon.cargurus.com' then 
                case
                    when SUBSTRING(email,charindex(';',email) + 1,len(email)) like '%@anon.cargurus.com' then NULL
                    else SUBSTRING(email,charindex(';',email) + 1,len(email))
                end
            when SUBSTRING(email,0,charindex(';',email)) like '%@geico.zagdealer.com' then 
                case
                    when SUBSTRING(email,charindex(';',email) + 1,len(email)) like '%@geico.zagdealer.com' then NULL
                    else SUBSTRING(email,charindex(';',email) + 1,len(email))
                end
            when SUBSTRING(email,0,charindex(';',email)) like '%@truecarcustomer.com' then 
                case 
                    when SUBSTRING(email,charindex(';',email) + 1,len(email)) like '%@truecarcustomer.com' then NULL
                    else SUBSTRING(email,charindex(';',email) + 1,len(email))
                end
            when SUBSTRING(email,0,charindex(';',email)) like '%@carbuyingemail.com' then 
                case
                    when SUBSTRING(email,charindex(';',email) + 1,len(email)) like '%@carbuyingemail.com' then NULL
                    else SUBSTRING(email,charindex(';',email) + 1,len(email))
                end
            else SUBSTRING(email,0,charindex(';',email))
        end
    else email
end test,
num_semi
from #tmp1

我正在使用 T-SQL。有什么建议吗?

一个更好的 IMO 方法是创建一个具有有效电子邮件地址的 table,然后使用 string split function..(假设电子邮件地址由 ;

分隔
declare @string varchar(max)
set @string='yyyyyyyy.xxxxxxxxx.p4elr874@carbuyingemail.com;yyyyyyyy.xxxxxxxxx.daojffab@carbuyingemail.com;xxxxxxxxx@aol.com'


create table validemails
(
emailid varchar(50)
)

insert into validemails
select '@aol.com'

;with cte
as
(select * from 
[dbo].[SplitStrings_Numbers](@string,';') 
)
select * from cte c
cross apply
(
select * from validemails b where c.item like '%'+b.emailid+''
) d