• Your Name Here (1/4/2013)


    @GSquared:

    Sorry to be thick-headed (read: slow-witted) but I have to ask: why did you SELECT TOP (8000) ROW_NUMBER... when it [seems] to work with TOP (10). Since we're using only the numbers 0-9, I would have figured using a generic "number generator" like SELECTING ROW_NUMBER() ... from a "known" source that would have at least 10 records (like sys.objects), you could skinny down the code and maybe processing as well:

    DECLARE @String VARCHAR(8000) = 'ABC12D34E56';

    SELECT (

    SELECT SUBSTRING(@String, n.[Number], 1)

    FROM (

    SELECT TOP (10) ROW_NUMBER()

    OVER (ORDER BY [object_id]) AS [Number]

    FROM sys.objects) n

    WHERE n.[Number] <= LEN(@String)

    AND SUBSTRING(@String, n.[Number], 1) LIKE '[0-9]'

    ORDER BY n.[Number]

    FOR XML PATH(''), TYPE).[value]('.[1]', 'VARCHAR(8000)');

    Factoring in inflation, health care, taxes and cost of living, my two cents isn't worth much so I apologize if I missed something. I'll blame the lack of coffee for any oversight on my part.

    Cheers,

    Ken

    Try it on a much larger string and see why the 8000 becomes important.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)