Pages

Social Icons

Monday 25 November 2013

Sorting comma separated values

Hi,

Below code will sort the comma separated values.

for using this code we need to use fnSplit method.

DECLARE @list VARCHAR(100)
set @list = '11,25,20,95,42,35,61,70,101,95,96,28';
WITH ComaSeparated AS
(
SELECT  *
FROM    dbo.fnSplit(@list,',')
)

SELECT TOP 1 SUBSTRING((SELECT ',' + item 
FROM ComaSeparated t2 
ORDER BY CAST(item AS INT) 
FOR XML PATH('')),2,LEN(@list)+1) FROM ComaSeparated