• ScottPletcher - Thursday, February 8, 2018 11:44 AM

    [
    You're welcome.  [To be fair, I used a tally table to generate the CASE statements; nobody familiar with tally tables writes that kind of stuff by hand.]

    I also used a specialized tally table (starting with 0 and steps of 3) and came up with a simpler approach.  (I thought that using a 0-base was more obvious than using a 1-base when incrementing by three, so I used that even though it made the formulas a little more complicated.)


    SELECT Column1, STUFF(ss,1, 1, '')
    FROM MyTable mt
    CROSS APPLY
    (
        SELECT '|' + SUBSTRING(mt.Column1, n + 1, 3)
        FROM ( VALUES(0), (3), (6), (9), (12), (15), (18), (21), (24) ) Tally(n)
        WHERE SUBSTRING(mt.Column1, n + 1, 3) > '000'
        ORDER BY n
        FOR XML PATH('')
    ) v(ss)

    Drew

    Edit: I didn't feel like entering the entire tally table, so you'll need to fill it out to 57.

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA