Skip to content

Instantly share code, notes, and snippets.

@clintmjohnson
Last active August 29, 2015 14:21
Show Gist options
  • Save clintmjohnson/f15a84f48076544dc1fc to your computer and use it in GitHub Desktop.
Save clintmjohnson/f15a84f48076544dc1fc to your computer and use it in GitHub Desktop.
Convert Vertical Row value to Comma Separated Values Using STUFF SQL SERVER
DECLARE @Heroes TABLE (
[HeroName] VARCHAR(20)
)
INSERT INTO @Heroes ( [HeroName] )
VALUES ( 'Superman' ), ( 'Batman' ), ('Ironman'), ('Wolverine')
--SELECT * FROM @Heroes
SELECT STUFF((SELECT ',' + [HeroName]
FROM @Heroes
ORDER BY [HeroName]
FOR XML PATH('')), 1, 1, '') AS [Output]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment