加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

如何从SQL Server中的单个行中提取多个字符串

发布时间:2020-12-12 16:21:20 所属栏目:MsSql教程 来源:网络整理
导读:我有例如下表数据: id | text--------------------------------------------------------------------------------1 | Peter (Peter@peter.de) and Marta (marty@gmail.com) are doing fine.2 | Nothing special here3 | Another email address (me@my.com)
我有例如下表数据:
id    |    text
--------------------------------------------------------------------------------
1     |  Peter (Peter@peter.de) and Marta (marty@gmail.com) are doing fine.
2     |  Nothing special here
3     |  Another email address (me@my.com)

现在我需要一个select来返回我的文本列中的所有电子邮件地址(可以只检查括号),如果text列中有多个地址,则返回多行.我知道how to extract the first element,但我完全不知道如何找到第二个和更多的结果.

解决方法

您可以递归使用cte去除字符串.
declare @T table (id int,[text] nvarchar(max))

insert into @T values (1,'Peter (Peter@peter.de) and Marta (marty@gmail.com) are doing fine.')
insert into @T values (2,'Nothing special here')
insert into @T values (3,'Another email address (me@my.com)')

;with cte([text],email)
as
(
    select
        right([text],len([text]) - charindex(')',[text],0)),substring([text],charindex('(',0) + 1,charindex(')',0) - charindex('(',0) - 1) 
    from @T
    where charindex('(',0) > 0
    union all
    select
        right([text],0) - 1) 
    from cte
    where charindex('(',0) > 0
)
select email
from cte

结果

email
Peter@peter.de
me@my.com
marty@gmail.com

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读