I am using sql server 2008 r2 with php in my website. I have 2 tables.
1 is for employees.
(int) (nvarchar) (nvarchar)
id name
Here's one way you can split a comma delimited list using For XML
:
SELECT w.workid, w.name,
STUFF((
SELECT ',' + E.Name AS [text()]
FROM (
SELECT A.workid,
Split.a.value('.', 'VARCHAR(100)') AS EmpId
FROM
(SELECT workid,
CAST ('' + REPLACE(employees, ',', ' ') + ' ' AS XML) AS String
FROM work
) AS A
CROSS APPLY String.nodes ('/M') AS Split(a)
) A
JOIN employees E ON A.EmpId = E.Id
WHERE WorkId = w.WorkId
FOR XML PATH('')
), 1, 1, '') AS Employees
FROM work w
SQL Fiddle Demo
This results in:
WORKID NAME EMPLOYEES
1 task1 john,leah
2 task2 peter,leah
3 task3 john,leah,frank
4 task4 peter