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

  • It's easy to combine both techniques. Here's an example returning rows with 4 characters or less.

    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(s.Item, (40 * (t.n-1)) + 1, 40 ),

    LEN(s.Item),

    LEN(SUBSTRING(s.Item, (40 * (t.n-1)) + 1, 40 ))

    FROM tableA a

    CROSS APPLY dbo.DelimitedSplit8K( wptext, CHAR(10)) s

    JOIN cteTally t ON t.n <= CEILING( LEN( s.Item) / 40.0)

    ORDER BY ItemNumber, 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