SQL通过逗号分隔的电子邮件地址拆分电子邮件数据

时间:2013-10-15 12:41:15

标签: sql sql-server

假设我有以下名称和逗号分隔的电子邮件地址列表:

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

是否可以为每个电子邮件地址选择一行?

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

1 个答案:

答案 0 :(得分: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)