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

时间:2018-06-27 03:50:02

标签: sql tsql sql-server-2014

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

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   

2 个答案:

答案 0 :(得分:2)

这是一种内联方法

示例

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 '%@%.___'

返回

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
  

编辑以单行返回

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

返回

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

答案 1 :(得分:1)

尝试此功能

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

它将拆分为逗号或空格

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