Concatenate Multiple row String into One Row

i found this useful script while finishing one of my work. XML Path is one of SQL Server clause that will help you to concatenate multiple row string into one row. hope below example will helps you too:

IF OBJECT_ID('TEMPDB..#TEMP') IS NOT NULL
 DROP TABLE #TEMP
 
CREATE TABLE #TEMP (COL1 VARCHAR(50))
INSERT INTO #TEMP
 SELECT 'COL1' UNION ALL
 SELECT 'COL2' UNION ALL
 SELECT 'COL3' UNION ALL
 SELECT 'COL4' UNION ALL
 SELECT 'COL5' 
 
SELECT ',' + COL1
FROM #TEMP
FOR XML PATH('')

CMIIW 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Afiseaza emoticoanele Locco.Ro