String Split

  • Comments posted to this topic are about the item String Split

  • Yeah I used such function a lot in our applications although it is nice to have the ability to call with different delimiters (sometimes people don't like to use commas :-)...)

    But recently I have been looking at it from a different point of view by using CLR functions and the speed of it is mind blowing compare to the T-SQL code.

    Only limitation though is that the CLR function doesn't accept a string as long as the T-SQL one (or maybe I haven't found the correct configuration yet.

    CLR might be very useful in this case so I wanted to give you the code if you want to try it.

    Cheers

    public partial class UserDefinedFunctions

    {

    [SqlFunction(Name = "ufn_SplitString", FillRowMethodName = "SplitString_FillRow", TableDefinition = "ID NVARCHAR(255)")]

    public static IEnumerable SqlArray(SqlString str, SqlChars delimiter)

    {

    if (delimiter.Length == 0)

    return new string[1] { str.Value };

    return str.Value.Split(delimiter[0]);

    }

    public static void SplitString_FillRow(object row, out SqlString str)

    {

    str = new SqlString((string)row);

    }

    };

  • Have a look at http://www.sqlservercentral.com/articles/Tally+Table/72993/

    (Tally OH! An Improved SQL 8K “CSV Splitter” Function)

    Especially, review the discussion thread. You can do this without committing that cardinal sin, looping (see references to the "Church of No RBAR" in the discussion). Performance is much improved when you avoid loops.


    Cursors are useful if you don't know SQL

  • Nice Post:smooooth:, worked like a charm for me 😀

  • You can also do this with help of below script:-

    DECLARE @CommString Varchar(100) ='1,2,3,4,5,6,7,8,9,10'

    SELECT CP_XML.Node.value('.','Varchar(max)') AS DATA FROM

    (

    SELECT DataIn_XML = Convert(XML,'<Node>'+REPLACE(@CommString,',','</Node><Node>')+'</Node>')

    ) AS NowDataIn_XmlFormat

    CROSS APPLY DataIN_XML.nodes('Node') CP_XML(Node)

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Thanks for the script.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply