在这里,您可以使用函数中的 cte 来完成它。
create function dbo.FixupEmails(@s varchar(8000))
returns table
as
return (
WITH splitter_cte AS (
SELECT CHARINDEX(';', @s) as pos, 0 as lastPos, 1 as cte_level
UNION ALL
SELECT CHARINDEX(';', @s, pos + 1), pos, cte_level + 1 as cte_level
FROM splitter_cte
WHERE pos > 0
), each_email_cte AS(
select replace(replace(replace(OneEmail, '>', ''), '<', ''), ' ', '') as OneEmail, cte_level
from (select SUBSTRING(@s, lastPos + 1,
case when pos = 0 then 80000 else pos - lastPos -1 end) as OneEmail,
cte_level
from splitter_cte) as t
), each_half_cte AS (
select OneEmail, CHARINDEX('@', OneEmail) as atPos, cte_level
from each_email_cte
where len(OneEmail) > 6 -- 6 from x@x.co (I think that 6 would be the minimum valid email length)
), new_email_cte as
(
select cte1.OneEmail, Replace(@s, cte1.OneEmail, 'TestMail@RandMail.com') as New, cte1.cte_level --, 1 as level
from each_half_cte cte1
where cte1.cte_level = 1
UNION ALL
select cte2.OneEmail, Replace(necte.New, cte2.OneEmail, 'TestMail@RandMail.com') as New, cte2.cte_level--, 1 as level
from new_email_cte as necte
inner join each_half_cte as cte2 on cte2.cte_level = necte.cte_level + 1
)
select New
from new_email_cte
where cte_level = (select max(cte_level) from new_email_cte)
)
go
set nocount on;
declare @emailString varchar(2048)
set @emailString = '<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com; comfarmer@yahoo.com ';
select @emailString as Original;
SELECT *
FROM dbo.FixupEmails(@emailString);
set @emailString = '<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com;';
select @emailString as Original;
SELECT *
FROM dbo.FixupEmails(@emailString);
set @emailString = '<JimmyTheBoot@yahoo.com>';
select @emailString as Original;
SELECT *
FROM dbo.FixupEmails(@emailString)
OPTION(MAXRECURSION 0);
-- include MAXRECURSION as shown above if you have more than 100 email addresses in the field.
set @emailString = '<bill@whatever.co.uk>; John@TestMail.tv;';
select @emailString as Original;
SELECT *
FROM dbo.FixupEmails(@emailString)
它有点长,但这是输出。
Original
----------------------------------------------------------------
<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com; comfarmer@yahoo.com
New
-----------------------------------------------------------------
<TestMail@RandMail.com>; TestMail@RandMail.com; TestMail@RandMail.com
Original
----------------------------------------------------------------
<JimmyTheBoot@yahoo.com>; JohnBlaze@TestMail.com;
New
----------------------------------------------------------------
<TestMail@RandMail.com>; TestMail@RandMail.com;
Original
----------------------------------------------------------------
<JimmyTheBoot@yahoo.com>
New
----------------------------------------------------------------
<TestMail@RandMail.com>
Original
----------------------------------------------------------------
<bill@whatever.co.uk>; John@TestMail.tv;
New
----------------------------------------------------------------
<TestMail@RandMail.com>; TestMail@RandMail.com;
这很有趣。我认为提供的功能将满足您的需求。