Home Forums SQL Server 2005 T-SQL (SS2K5) split column into multiple records based on substring fixed lenght RE: split column into multiple records based on substring fixed lenght

  • A Tally table (or in this case a cteTally) can come to the rescue.

    Check the following code and ask for anything that you don't understand.

    WITH E1(n) AS(

    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1

    UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1

    ),

    cteTally(n) AS(

    SELECT TOP 50 ROW_NUMBER() OVER( ORDER BY ( SELECT NULL))

    FROM E1, E1 b

    )

    SELECT cust, SUBSTRING(wptext, (80 * (n-1)) + 1, 80 ) AS wptext, n as seq

    FROM TableA a

    JOIN cteTally t ON t.n <= CEILING( LEN( a.wptext) / 80.0)

    order by cust, n

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2