I have e.g. the following table data:
id | text
--------------------------------------------------------------------------------
1 | Peter (Peter@
You can use a cte recursively to strip out the strings.
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
Result
email
Peter@peter.de
me@my.com
marty@gmail.com
THe substring functions have starting position parameter. So you find the first occurrence,and start the next search (in your loop) at the occurrence position + occurenceLength. You'd need to write a function that returns the values either as a delimited string or table. Use the @-sign to find your way into the email address, and then scan backwards and forwards until you reach white space or a character that's invalid in an email address (or the start-pos or the beginning or the last char).
This assumes there are no rogue parentheses and you would need to add some additional replace
s in if your text can contain any XML entity characters.
WITH basedata(id, [text])
AS (SELECT 1, 'Peter (Peter@peter.de) and Marta (marty@gmail.com) are doing fine.'
UNION ALL
SELECT 2, 'Nothing special here'
UNION ALL
SELECT 3, 'Another email address (me@my.com)'),
cte(id, t, x)
AS (SELECT *,
CAST('<foo>' + REPLACE(REPLACE([text],'(','<bar>'),')','</bar>') + '</foo>' AS XML)
FROM basedata)
SELECT id,
a.value('.', 'nvarchar(max)') as address
FROM cte
CROSS APPLY x.nodes('//foo/bar') as addresses(a)