gpt4 book ai didi

string - MS SQL - 如何从大字符串中获取以逗号/分号分隔的电子邮件地址列表

转载 作者:行者123 更新时间:2023-12-04 05:34:57 26 4
gpt4 key购买 nike

如何提取字符串中可用的所有电子邮件地址的列表,作为逗号/分号分隔的列表

SELECT dbo.getEmailAddresses('this is misc andrew@g.com')

--output andrew@g.com

SELECT dbo.getEmailAddresses('this is misc andrew@g.com and a medium text returning %John@acme.com')
--output andrew@g.com; John@acme.com

最佳答案

这是一个 UDF。更正此行 SET @MailTempl='[A-Za-z0-9_.-]如果您需要其他(例如电子邮件地址中的非英文符号)。 Here is a full list of allowed characters

CREATE FUNCTION [dbo].[getEmailAddresses] (@Str varchar(8000))  
RETURNS varchar(8000) AS
BEGIN

declare @i int, @StartPos int,@AtPos int,@EndPos int;
declare @MailList varchar(8000);
declare @MailTempl varchar(100);


SET @MailList=NULL;
SET @MailTempl='[A-Za-z0-9_.-]'; --allowing symbols in e-mail not including @

SET @AtPos=PATINDEX('%'+@MailTempl+'@'+@MailTempl+'%',@Str)+1;
While @AtPos>1
begin
--go left
SET @i=@AtPos-1;
while (substring(@Str,@i,1) like @MailTempl) SET @i=@i-1;
SET @StartPos=@i+1;

--go right
SET @i=@AtPos+1;
while (substring(@Str,@i,1) like @MailTempl) SET @i=@i+1;
SET @EndPos=@i-1;

SET @MailList=isnull(@MailList+';','')+Substring(@Str,@StartPos,@EndPos-@StartPos+1);

--prepare for the next round
SET @Str=substring(@Str,@EndPos+1,LEn(@Str));
SET @AtPos=PATINDEX('%'+@MailTempl+'@'+@MailTempl+'%',@Str)+1;

end;

RETURN @MailList;

END

关于string - MS SQL - 如何从大字符串中获取以逗号/分号分隔的电子邮件地址列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12085326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com