Split string in database row and copy results to different rows - SQL Server

前端 未结 3 1624
暗喜
暗喜 2021-01-16 14:58

I created an application to send mail to users. I\'m using a database where some users have more than one email address assigned.

Name                                


        
3条回答
  •  无人及你
    2021-01-16 15:31

    Here's a simple example using the XML features in SQL Server 2005 and above. I've taken it verbatim from here but there are lots of examples if you google "split string sql server xml"

    DECLARE @xml as xml,@str as varchar(100),@delimiter as varchar(10)
    SET @str='A,B,C,D,E'
    SET @delimiter =','
    SET @xml = cast((''+replace(@str,@delimiter ,'')+'') as xml)
    SELECT N.value('.', 'varchar(10)') as value FROM @xml.nodes('X') as T(N)
    

    There are other solutions with cursors but this approach as worked well for me.

提交回复
热议问题