如果字符串是列中的电子邮件格式,我该如何提取?

How do I extract if the strings are in email format from a column??

我想提取特定列中包含的不同电子邮件。该列可能有其他类型的字符串,这些字符串可能不是我想排除的电子邮件,我只想要电子邮件。

Table 1 
ID         Value 
1          Sent Email successfully to John.Muller@gmail.com, Jim.James@gmail.com, Bob.King@hotmail.com and it will be sent tomorrow
2          Email not successful to adam.sandy@yahoo.com, Trisha.stratus@gmail.com, lindy.123@gmail.com  and it will not be sent today

如果我们看到这种格式,我的每个 ID 都有 3 个不同的电子邮件,我想知道是否有任何方法可以获取这种格式的数据。

  Expected output 
ID         Value 
1          John.Muller@gmail.com, Jim.James@gmail.com, Bob.King@hotmail.com  
2          adam.sandy@yahoo.com, Trisha.stratus@gmail.com, lindy.123@gmail.com   

要是也可以这样输出就好了。

  Another Possible Output (this would be awesome if possible) 
ID         Value 
1          John.Muller@gmail.com 
1          Jim.James@gmail.com 
1          Bob.King@hotmail.com  
2          adam.sandy@yahoo.com 
2          Trisha.stratus@gmail.com 
2          lindy.123@gmail.com   

试试这个功能

alter FUNCTION [dbo].[FnSplit2]
(
@List nvarchar(max),
@SplitOn1 nvarchar(5),
@SplitOn2 nvarchar(5)

)  
--Akram Mustafa
RETURNS @RtnValue table 
(

Id int identity(1,1),
Value nvarchar(1000)
) 
AS  
BEGIN
declare @SplitOn varchar(5)
While (Charindex(@SplitOn1,@List)>0  or Charindex(@SplitOn2,@List)>0 )
Begin 
if Charindex(@SplitOn1,@List)<Charindex(@SplitOn2,@List) and Charindex(@SplitOn1,@List)> 0
begin
    set @SplitOn = @SplitOn1
end
else
begin
    set @SplitOn = @SplitOn2
end
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1))) 
Set @List = Substring(@List,Charindex(@SplitOn,@List)+DATALENGTH(@SplitOn),DATALENGTH(@List))
End 

Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END

它将拆分为逗号或白色 space

declare @MyTable as table (id int, value varchar(1000))
insert into @MyTable(id, value)
values(1, 'Sent Email successfully to John.Muller@gmail.com, Jim.James@gmail.com, Bob.King@hotmail.com and it will be sent tomorrow')

insert into @MyTable(id, value)
values(2, 'Email not successful to adam.sandy@yahoo.com, Trisha.stratus@gmail.com, lindy.123@gmail.com  and it will not be sent today')

declare @str varchar(max)

 SELECT @str = LEFT(Value, LEN(Value) - 1)
FROM (
    SELECT Value + ', '
    FROM @MyTable
    FOR XML PATH ('')
  ) c (Value)

 select value from dbo.fnSplit2(@str,',',' ')
 where value like '%@%'

最终结果将是

value
John.Muller@gmail.com
Jim.James@gmail.com
Bob.King@hotmail.com
adam.sandy@yahoo.com
Trisha.stratus@gmail.com
lindy.123@gmail.com

这是一个内联方法

例子

Select A.ID
      ,Value = B.RetVal
 From  YourTable A
 Cross Apply (
                Select RetSeq = Row_Number() over (Order By (Select null))
                      ,RetVal = v.value('(./text())[1]', 'varchar(150)')
                 From  (values (convert(xml,'<x>' + replace(replace(Value,' ',','),',','</x><x>')+'</x>'))) x(n)
                 Cross Apply n.nodes('x') node(v)
              ) B
 Where RetVal like '%@%.___'

Returns

ID  Value
1   John.Muller@gmail.com
1   Jim.James@gmail.com
1   Bob.King@hotmail.com
2   adam.sandy@yahoo.com
2   Trisha.stratus@gmail.com
2   lindy.123@gmail.com

Edit To return as one line

Select A.ID
      ,B.Value 
 From  YourTable A
 Cross Apply (
                Select Value = Stuff((Select ', ' +RetVal 
                  From (
                        Select RetSeq = Row_Number() over (Order By (Select null))
                              ,RetVal = v.value('(./text())[1]', 'varchar(150)')
                         From  (values (convert(xml,'<x>' + replace(replace(Value,' ',','),',','</x><x>')+'</x>'))) x(n)
                         Cross Apply n.nodes('x') node(v)
                       ) B1
                  Where RetVal like '%@%.___'
                  Order by RetSeq
                  For XML Path ('')),1,2,'') 
              ) B

Returns

ID  Value
1   John.Muller@gmail.com, Jim.James@gmail.com, Bob.King@hotmail.com
2   adam.sandy@yahoo.com, Trisha.stratus@gmail.com, lindy.123@gmail.com