MS SQL Server Build coalesce string with loop from table

I have a table I need to build a comma separated string from

This is the result I need
1-5,9,12,15-17

From this list example

1
2
3
4
5
9
12
15
16
17
Larry Bristersr. DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
Itzik Ben-Gan addresses this in his "islands" SQL examples.

If you can provide usable sample data -- CREATE TABLE, INSERT statements -- rather than just a list of values, I can provide a good answer based on an "islands" solution :-).
0
ste5anSenior DeveloperCommented:
Do an islands and gap search, then create your result.

Using window functions makes it pretty simple:

DECLARE @List TABLE
    (
        n INT
    );

INSERT INTO @List ( n )
VALUES ( 1 ) ,
       ( 2 ) ,
       ( 3 ) ,
       ( 4 ) ,
       ( 5 ) ,
       ( 9 ) ,
       ( 12 ) ,
       ( 15 ) ,
       ( 16 ) ,
       ( 17 );

WITH Starts
AS ( SELECT L.n ,
            IIF(L.n - ISNULL(LAG(L.n, 1, NULL) OVER ( ORDER BY L.n ASC ), -9999) > 1, 1, 0) AS newIsland
     FROM   @List L ) ,
     Numbered
AS ( SELECT S.n ,
            SUM(S.newIsland) OVER ( ORDER BY S.n ASC ) AS islandNo
     FROM   Starts S ) ,
     Aggregated
AS ( SELECT   CAST(MIN(N.n) AS VARCHAR(255)) AS islandStart ,
              CAST(MAX(N.n) AS VARCHAR(255)) AS islandEnd ,
              MAX(N.n) - MIN(N.n) + 1 AS islandSize ,
              N.islandNo
     FROM     Numbered N
     GROUP BY N.islandNo )
SELECT STUFF((   SELECT   ', ' + IIF(A.islandSize = 1, A.islandStart, A.islandStart + '-' + A.islandEnd)
                 FROM     Aggregated A
                 ORDER BY A.islandNo
                 FOR XML PATH('')) ,
             1 ,
             2 ,
             '') AS islands;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Larry Bristersr. DeveloperAuthor Commented:
Thanks.

This is my final result function

SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- ================================================================
-- Author:            Larry Brister
-- Create date: 01.15.2018
-- Description:      Get "scaled" list from comma seperated string
-- select dbo.fncReturnScaledString('7654,7653,7650,7604,7605,7606,')
-- ================================================================
ALTER FUNCTION fncReturnScaledString
    (
        -- Add the parameters for the function here
        @NumList VARCHAR(MAX)
    )
RETURNS VARCHAR(1000)
AS
    BEGIN
      DECLARE @ResultVar VARCHAR(1000);
        -- Declare the return variable here
        DECLARE @List TABLE
            (
                n INT
            );

        INSERT INTO @List ( n )
                    SELECT   [str]
                    FROM     dbo.ParseList(@NumList, ',')
                    ORDER BY [str];
        WITH Starts
        AS ( SELECT L.n ,
                    IIF(L.n - ISNULL(LAG(L.n, 1, NULL) OVER ( ORDER BY L.n ASC ), -9999) > 1, 1, 0) AS newIsland
             FROM   @List L ) ,
             Numbered
        AS ( SELECT S.n ,
                    SUM(S.newIsland) OVER ( ORDER BY S.n ASC ) AS islandNo
             FROM   Starts S ) ,
             Aggregated
        AS ( SELECT   CAST(MIN(N.n) AS VARCHAR(255)) AS islandStart ,
                      CAST(MAX(N.n) AS VARCHAR(255)) AS islandEnd ,
                      MAX(N.n) - MIN(N.n) + 1 AS islandSize ,
                      N.islandNo
             FROM     Numbered N
             GROUP BY N.islandNo )
        SELECT @ResultVar =  STUFF((   SELECT   ', ' + IIF(A.islandSize = 1, A.islandStart, A.islandStart + '-' + A.islandEnd)
                         FROM     Aggregated A
                         ORDER BY A.islandNo
                         FOR XML PATH('')) ,
                     1 ,
                     2 ,
                     '');
        -- Return the result of the function
        RETURN @ResultVar;
    END;
0
ste5anSenior DeveloperCommented:
Contains ParseList() a sanity check? Also consider using VARCHAR(MAX) as result type, cause from MAX as input you may get a result longer than 1000 characters.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.