Tag Archives: Sorted and Order by clause

T-SQL – Query to get Distinct,Sorted, Comma Separated values in to a variable

CREATE TABLE #temp(name char(3)) INSERT INTO #temp VALUES (‘CD’) ,(‘AB’) ,(‘LM’) ,(‘BC’) ,(‘GH’) ,(‘KJ’) ,(‘AB’) DECLARE @cols AS NVARCHAR(MAX); SELECT @COLS = substring(list, 1, len(list) – 1) FROM (SELECT list = (SELECT DISTINCT name + ‘,’ FROM #temp ORDER BY … Continue reading

Posted in T-SQL | Tagged , , , | 10 Comments