STUFF function to display multiple columns with comma-separated in SQL Server
问题 I need to display a table column (assume 2 columns) values with comma-separated in the output col1 Col2 ================ xyz abc pqr uvw Output needed: Col1 Col2 ======== ======= xyz,pqr abc,uvw I tried below query but not sure how to include col2 to the below query and make it as a single query: SELECT STUFF((SELECT DISTINCT ',' + CAST(t.Col1 AS VARCHAR(MAX)) FROM Table_1 t FOR XML PATH('')), 1, 1, '') 回答1: SQL Fiddle MS SQL Server 2017 Schema Setup : create table MyTable(ColA varchar(max)