Sql 返回特定字符周围的文本

Sql 返回特定字符周围的文本,sql,sql-server,Sql,Sql Server,我在包含电子邮件地址的表中有一个自由格式的文本字段。问题是在这个领域有很多其他信息,在电子邮件地址之前和之后。 编辑:下面是一个字段的示例: Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info... 我认为尝试提取电子邮件地址的最好方法是使用SQL搜索@字符,并检索所有周围的文本,直到找到一个空格。这可能吗?或者有更好的方法吗?这里有一种方法 SELECT Emai

我在包含电子邮件地址的表中有一个自由格式的文本字段。问题是在这个领域有很多其他信息,在电子邮件地址之前和之后。 编辑:下面是一个字段的示例:

Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info...

我认为尝试提取电子邮件地址的最好方法是使用SQL搜索@字符,并检索所有周围的文本,直到找到一个空格。这可能吗?或者有更好的方法吗?

这里有一种方法

SELECT Email = Reverse(Substring(Reverse(string), revpos+1, Charindex(' ', Reverse(string), revpos)-revpos))
               + Substring(string, pos, Charindex(' ', string, pos)-pos)
FROM   yourtable
       CROSS apply(VALUES (Charindex('@', string)))tc(pos)
       CROSS apply(VALUES (Charindex('@', Reverse(string))))tc1(revpos) 
注:

这假设了两件事

  • 始终只有一个
    @
    存在
  • 电子邮件
    总是被空格包围

  • 这里有一种方法

    SELECT Email = Reverse(Substring(Reverse(string), revpos+1, Charindex(' ', Reverse(string), revpos)-revpos))
                   + Substring(string, pos, Charindex(' ', string, pos)-pos)
    FROM   yourtable
           CROSS apply(VALUES (Charindex('@', string)))tc(pos)
           CROSS apply(VALUES (Charindex('@', Reverse(string))))tc1(revpos) 
    
    注:

    这假设了两件事

  • 始终只有一个
    @
    存在
  • 电子邮件
    总是被空格包围
  • 根据回答(Stephan于2015年4月13日2:44) 看到这个了吗

    查询1

    select BigString, c3.email
    from (
        select BigString
        from Table1
        where CHARINDEX('@',BigString) > 0
        ) t
    cross apply (select charindex('@',BigString)) c1 (atpos)
    cross apply (select charindex(' ',BigString + ' ',c1.atpos), reverse(substring(BigString,1,c1.atpos))
                ) c2 (endOfEmail,RevString)  
    cross apply (select RTRIM(Reverse(left(RevString,charindex(' ',RevString+' ')-1))
                      + substring(BigString,c1.atpos+1,endOfEmail-c1.atpos))
                ) c3 (email)
    
    |                                                                                     BigString |                     email |
    |-----------------------------------------------------------------------------------------------|---------------------------|
    | Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info... |        person@example.com |
    |                                                                blah MyEmailAddress@domain.org | MyEmailAddress@domain.org |
    |                                                      blah MyEmailAddress@domain.org blah blah | MyEmailAddress@domain.org |
    |                                                                MyEmailAddress@domain.org blah | MyEmailAddress@domain.org |
    

    select BigString, c3.email
    from (
        select BigString
        from Table1
        where CHARINDEX('@',BigString) > 0
        ) t
    cross apply (select charindex('@',BigString)) c1 (atpos)
    cross apply (select charindex(' ',BigString + ' ',c1.atpos), reverse(substring(BigString,1,c1.atpos))
                ) c2 (endOfEmail,RevString)  
    cross apply (select RTRIM(Reverse(left(RevString,charindex(' ',RevString+' ')-1))
                      + substring(BigString,c1.atpos+1,endOfEmail-c1.atpos))
                ) c3 (email)
    
    |                                                                                     BigString |                     email |
    |-----------------------------------------------------------------------------------------------|---------------------------|
    | Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info... |        person@example.com |
    |                                                                blah MyEmailAddress@domain.org | MyEmailAddress@domain.org |
    |                                                      blah MyEmailAddress@domain.org blah blah | MyEmailAddress@domain.org |
    |                                                                MyEmailAddress@domain.org blah | MyEmailAddress@domain.org |
    
    根据回答(Stephan于2015年4月13日2:44) 看到这个了吗

    查询1

    select BigString, c3.email
    from (
        select BigString
        from Table1
        where CHARINDEX('@',BigString) > 0
        ) t
    cross apply (select charindex('@',BigString)) c1 (atpos)
    cross apply (select charindex(' ',BigString + ' ',c1.atpos), reverse(substring(BigString,1,c1.atpos))
                ) c2 (endOfEmail,RevString)  
    cross apply (select RTRIM(Reverse(left(RevString,charindex(' ',RevString+' ')-1))
                      + substring(BigString,c1.atpos+1,endOfEmail-c1.atpos))
                ) c3 (email)
    
    |                                                                                     BigString |                     email |
    |-----------------------------------------------------------------------------------------------|---------------------------|
    | Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info... |        person@example.com |
    |                                                                blah MyEmailAddress@domain.org | MyEmailAddress@domain.org |
    |                                                      blah MyEmailAddress@domain.org blah blah | MyEmailAddress@domain.org |
    |                                                                MyEmailAddress@domain.org blah | MyEmailAddress@domain.org |
    

    select BigString, c3.email
    from (
        select BigString
        from Table1
        where CHARINDEX('@',BigString) > 0
        ) t
    cross apply (select charindex('@',BigString)) c1 (atpos)
    cross apply (select charindex(' ',BigString + ' ',c1.atpos), reverse(substring(BigString,1,c1.atpos))
                ) c2 (endOfEmail,RevString)  
    cross apply (select RTRIM(Reverse(left(RevString,charindex(' ',RevString+' ')-1))
                      + substring(BigString,c1.atpos+1,endOfEmail-c1.atpos))
                ) c3 (email)
    
    |                                                                                     BigString |                     email |
    |-----------------------------------------------------------------------------------------------|---------------------------|
    | Address: 123 Anystreet. Customer requires xyz. Email: person@example.com Other random info... |        person@example.com |
    |                                                                blah MyEmailAddress@domain.org | MyEmailAddress@domain.org |
    |                                                      blah MyEmailAddress@domain.org blah blah | MyEmailAddress@domain.org |
    |                                                                MyEmailAddress@domain.org blah | MyEmailAddress@domain.org |
    

    你确定电子邮件总是被空格包围吗?电子邮件地址的某些部分也有最大长度(我记不清了,这是RFC的一个古老标准…),但你的数据可能有多个用逗号或分号分隔的电子邮件地址吗?@GordonLinoff相当确定。让我们假设是的,总是这样。请参阅我添加到问题中的示例。@Used\u By\u已经没有了,很遗憾没有。数据乱七八糟,完全不一致。参见上面的例子。您是否尝试过类似问题中的任何方法?您是否确定电子邮件总是被空格包围?电子邮件地址的某些部分也有最大长度(我记不清了,RFC中有一个古老的标准…)但是你的数据会有多个用逗号或分号分隔的电子邮件地址吗?@GordonLinoff相当肯定。让我们假设是的,总是这样。请参阅我添加到问题中的示例。@Used\u By\u已经没有了,很遗憾没有。数据乱七八糟,完全不一致。参见上面的示例。您是否使用更大的样本数据集尝试过类似问题中的任何方法,当使用charindex避免无效长度错误时,此代码需要
    +'
    ,请参见使用更大的样本数据集,当使用charindex避免无效的长度错误时,此代码需要
    +'
    ,请参见我在询问之前搜索过的内容,但当然找不到任何内容。我的google fu肯定生锈了……谢谢你,我会试试看。我想我的google fu是按照“从长字符串中提取电子邮件”的思路编写的,但返回的垃圾太多了:)我在询问之前搜索过,但当然什么也找不到。我的google fu一定生锈了……谢谢你,我会试试看。我想我的google fu是按照“从长字符串中提取电子邮件”的思路写的,但返回的垃圾太多了:)