简体   繁体   English

如何从列中提取字符串是否为电子邮件格式?

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

I wanted to extract different emails included in a specific column. 我想提取特定列中包含的不同电子邮件。 The column may have other type of strings which may not be emails which I want to exclude and I just want emails. 该列可能具有其他类型的字符串,这些字符串可能不是我要排除的电子邮件,而我只是想要的电子邮件。

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

If we see this format, I have 3 different emails on each ID, and I wanted to know if there is any way which I could get the data in this format. 如果我们看到这种格式,则每个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   

If this type of output is also possible, it would be great. 如果这种类型的输出也是可能的,那就太好了。

  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   

Here is an inline approach 这是一种内联方法

Example

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

Try this Function 试试这个功能

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

it will split for either comma or white 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 '%@%'

end result will be 最终结果将是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM