Dan*_*iel 3 sql sql-server pattern-matching
我有例如下表数据:
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)
Run Code Online (Sandbox Code Playgroud)
现在我需要一个select来返回我的文本列中的所有电子邮件地址(可以只检查括号),如果text列中有多个地址,则返回多行.我知道如何提取第一个元素,但我对如何找到第二个和更多结果完全无能为力.
您可以递归使用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('(', [text], 0) + 1, charindex(')', [text], 0) - charindex('(', [text], 0) - 1)
from @T
where charindex('(', [text], 0) > 0
union all
select
right([text], len([text]) - charindex(')', [text], 0)),
substring([text], charindex('(', [text], 0) + 1, charindex(')', [text], 0) - charindex('(', [text], 0) - 1)
from cte
where charindex('(', [text], 0) > 0
)
select email
from cte
Run Code Online (Sandbox Code Playgroud)
结果
email
Peter@peter.de
me@my.com
marty@gmail.com
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8174 次 |
| 最近记录: |