• MackF (3/29/2013)


    I am now testing this one:

    CREATE FUNCTION fnSplitString(@str nvarchar(max),@sep nvarchar(max))

    RETURNS TABLE

    AS

    RETURN

    WITH a AS(

    SELECT CAST(0 AS BIGINT) as idx1,CHARINDEX(@sep,@str) idx2

    UNION ALL

    SELECT idx2+1,CHARINDEX(@sep,@str,idx2+1)

    FROM a

    WHERE idx2>0

    )

    SELECT SUBSTRING(@str,idx1,COALESCE(NULLIF(idx2,0),LEN(@str)+1)-idx1) as value

    FROM a

    Execution is more simple, and performs same than above with less CPU time consumed for optimizations then...

    Ma data to parse could be just like that 'toto.toto,toto.toto,toto.'

    Simple 🙂

    From your original post you stated that you wanted to find an alternative to an XML splitter.

    For some performance considerations, we would like to try alternative to the code below.

    I don't quite understand why you keep looking at slower alternatives than the one suggested. This is like going to a car dealer and telling them you want to have the fastest car on the lot. The guy show you the Lamborghini, you smile and nod your head and walk over the Pinto. You test drive it and it is in fact faster than the Pacer you are driving currently. The salesman reminds you that for the same price ($0 in t-sql land) that you could drive the MUCH MUCH MUCH faster Aventador but you say you want to keep testing out the Pinto. It just doesn't make sense. Read the article about the tally table splitter, look at the performance tests. There is some code that is almost identical to the one you posted. It was tested in that article and it was found to be magnitudes slower. Stop driving the Pinto and accept the free keys to your new high performance sports car.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/