How to extract multiple strings from single rows in SQL Server

前端 未结 3 1227
暖寄归人
暖寄归人 2020-12-19 23:01

I have e.g. the following table data:

id    |    text
--------------------------------------------------------------------------------
1     |  Peter (Peter@         


        
3条回答
  •  渐次进展
    2020-12-19 23:37

    This assumes there are no rogue parentheses and you would need to add some additional replaces 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('' + REPLACE(REPLACE([text],'(',''),')','') + '' AS XML)
             FROM   basedata)
    SELECT id,
           a.value('.', 'nvarchar(max)') as address
    FROM   cte
           CROSS APPLY x.nodes('//foo/bar') as addresses(a) 
    

提交回复
热议问题