Tuesday, 29 December 2009

SQL server concatenate

DECLARE @test TABLE (
FirstName varchar(50)
)
--
INSERT INTO @test
SELECT 'Jon'
UNION SELECT 'Tom'
UNION SELECT 'Mike'
--
SELECT STUFF((
SELECT DISTINCT ', ' + FirstName
FROM @test
FOR XML PATH('')
), 1, 2, '')
FOR XML does the concat, STUFF removes the first 2 characters from position 1 - i.e. the initial unwanted  ', '

No comments:

Post a Comment