Add a dash between running numbers and comma between non-running numbers : I would like to replace a set of running and non running numbers with commas and hyphens where appropriate.

Using STUFF & XML PATH I was able to accomplish some of what I want by getting something like 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 13, 15, 19, 20, 21, 22, 24.

    WITH CTE AS (  
    SELECT DISTINCT t1.ORDERNo, t1.Part, t2.LineNum  
    FROM [DBName].[DBA].Table1 t1    
    JOIN Table2 t2 ON t2.Part = t1.Part    
    WHERE t1.ORDERNo = 'AB12345') 
       
    SELECT c1.SQ, c1.Part, STUFF((SELECT ', ' + CAST(LineNum AS VARCHAR(5))  
    FROM CTE c2  
    WHERE c2.SQ = c1.SQ  
    FOR XML PATH('')), 1, 2, '') AS [LineNums]  
    FROM CTE c1  
    GROUP BY c1.SQ, c1.Part

I would like to have 1-10, 13, 15, 19, 20-22, 24.