How to concatenate row data by comma in sql?

How to concatenate row data by “,” in sql?

DECLARE @Account_Num VARCHAR(7500) = ”

CREATE TABLE #tempData
(
AccountNo VARCHAR(20)
)

INSERT INTO #tempData
SELECT ‘123456’
UNION
SELECT ‘89105’
UNION
SELECT ‘32587’
UNION
SELECT ‘98587’

SELECT @Account_Num += COALESCE(‘,’, @Account_Num) + AccountNo FROM #tempData
SET @Account_Num = SUBSTRING(@Account_Num, 2, LEN(@Account_Num)) SELECT @Account_Num

Result:

123456,89105,32587,98587

Leave a Reply

Protected with IP Blacklist CloudIP Blacklist Cloud

Unable to load the Are You a Human PlayThru™. Please contact the site owner to report the problem.