I have a table with 1 column of varchar values. I am looking for a way to concatenate those values into a single value without a loop, if possible. If a loop is the most e
try this:
DECLARE @YourTable table (Col1 int)
INSERT INTO @YourTable VALUES (1)
INSERT INTO @YourTable VALUES (2)
INSERT INTO @YourTable VALUES (30)
INSERT INTO @YourTable VALUES (400)
INSERT INTO @YourTable VALUES (12)
INSERT INTO @YourTable VALUES (46454)
SELECT
STUFF(
(
SELECT
', ' + cast(Col1 as varchar(30))
FROM @YourTable
WHERE Col1<=400
ORDER BY Col1
FOR XML PATH('')
), 1, 2, ''
)
OUTPUT:
-------------------
1, 2, 12, 30, 400
(1 row(s) affected)