0

Say I have the following list of names and comma separated email addresses:

SELECT 'a' name, 'bob1@bob.com, bob2@bob.com' email INTO #Temp UNION ALL
SELECT 'b', 'john@john.com' UNION ALL
SELECT 'c', 'pete@pete.com, andrew@andrew.com, will@will.com'

SELECT * FROM #temp

Is it possible to select a row per email address?

i.e.

a bob1@bob.com
a bob2@bob.com
b john@john.com
c pete@pete.com
c andrew@andrew.com
c will@will.com
4

1 回答 1

3

请试试:

SELECT A.name,  
     Split.a.value('.', 'VARCHAR(100)') AS CVS  
FROM  
(
    SELECT name,  
         CAST ('<M>' + REPLACE(email, ',', '</M><M>') + '</M>' AS XML) AS CVS  
    FROM  #Temp 
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
于 2013-10-15T12:45:28.820 回答