Retrieving Comma separated values for a column instead of multiple rows


SELECT u.intUserId,
(
SELECT  cast(Roles.vchrole AS varchar)+ ', '
FROM roles INNER JOIN user_roles ur ON ur.intRoleId=roles.intRoleId
INNER JOIN users u1 ON u1.intUserId=ur.intUserId
WHERE u.intUserId=u1.intUserId
FOR XMl PATH ('')
) AS AllRoles
FROM users  u
GROUP BY u.intUserId




Another Solution Below:

 SELECT @EmployeeIds = COALESCE(@EmployeeIds + ',', '') + CAST(EmployeeId AS VARCHAR(5))
      FROM Employees
      WHERE City = @City

Comments

Popular posts from this blog

TSQL To Get All the Names of tables and Databases whose data have changed in last 24 Hours

To Get All the Names of tables and Databases their data have changed in last 24 Hours And Backup those databases

Apply paging in procedure