I am using VS2005 C# and SQL2005.
I would like to retrieve data from multiple columns and merge them, separating them by comma.
E.g. Data in my SQL Table UserData:
No. | Username | Role 1 | Role 2 | Role 3 |
1 | Jimmy | USER | READONLY | WRITE |
Data I would like to display in my GridView GridView1:
No. | Username | Roles |
1 | Jimmy | USER, READONLY, WRITE |
How can I use a SELECT statement to merge data from the 3 columns and merge them, separating the data by commas and list them in the GridView?
Thank you
EDIT
I have tried using the method provided by Shark and Mun, but I would like to remove the commas if the data in a Role column is empty.
Situation now is that Roles column will look like this if Role 2 and Role 3 is empty:
USER,,
May I know how can I group , into the variable as well so it will not display if the value is empty?
ISNULL()to get your desired result.ISNULL()function and that should be what the OP is looking for.