I have a table
columns:Name,Role,Dept
A person can have many roles. So while retrieving the data i want the roles column to be in CSV if a person has more than one role.
Name Role Dept
jose Role1,Role2 PWD
Assuming all other details except the roles for the records are same.
Thanks
This will do the trick for you.
This is taken from the first example in “The blackbox XML methods” from this page
https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/
The inner query builds the list of Roles as an xml. The xml path(”) parameter is empty and therefor you do not get the xml markup but only the comma separated list.
The outer query is only there to remove the comma at the end of each line.
Group by t2.Name, t2.Deptmakes sure that you will only get one row for each unique combination of Name+Dept.