Wednesday, September 21, 2011

Constructing row values into comma-separate string

 ---Create Table
CREATE TABLE #USER (USERNAME VARCHAR(25))

---Inserting Values
INSERT INTO #USER (USERNAME) VALUES ('Loki')
INSERT INTO #USER (USERNAME) VALUES ('Chinnu')
INSERT INTO #USER (USERNAME) VALUES ('Chinni')
INSERT INTO #USER (USERNAME) VALUES ('Laddu')

---Constricting comma-separate string
SELECT DISTINCT STUFF((SELECT ',' +USERNAME  FROM #user  FOR XML PATH ('')),1,1,'')AS LIST FROM #user

--- Drop table
DROP TABLE #USER

No comments:

Post a Comment