Skip to content

Instantly share code, notes, and snippets.

@Tomamais
Created January 22, 2024 21:57
Show Gist options
  • Save Tomamais/1b5cd411f33a698e28b5b09efc417ff2 to your computer and use it in GitHub Desktop.
Save Tomamais/1b5cd411f33a698e28b5b09efc417ff2 to your computer and use it in GitHub Desktop.
-- Tested on SQL Server 2019
DECLARE @inputString NVARCHAR(100) = '1,3,2,4,2';
SELECT STUFF((
SELECT ',' + CAST(value AS NVARCHAR(MAX))
FROM STRING_SPLIT(@inputString, ',')
ORDER BY CAST(value AS INT)
FOR XML PATH('')
), 1, 1, '') AS SortedString;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment