Home Forums SQL Server 2008 T-SQL (SS2K8) How to parse a string to equal length substrings in SQL RE: How to parse a string to equal length substrings in SQL

  • This is one possibility:

    WITH E(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 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL

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

    )

    ,Tally(n) AS(

    SELECT (ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) * 6) - 5 FROM E a--, E b /*Uncomment for strings longer than 97 chars and up to 1536 chars long*/

    )

    select ID, name, SUBSTRING( CTVALUE1, n, 6), n

    from ptemp, Tally

    WHERE n < LEN( CTVALUE1);

    It seems that Lowell was faster than me.

    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