I have to split a string with '&' separated values without the use of a function or stored proc and data is not handled well when converted to XML

  • I simply need to separate the string for each row by the & character and then I'm assuming i ll be able to COUNT and GROUP BY the occurrences of each separate value in order to find the most commonly used inputs.

    I have a column Variables in the table Functions, that contains a string of values separated by the & character that shows the inputs each student inserted into a function.

    How would I go about splitting that string without the use of a function or stored procedure and the find the most commonly used variables? (I was thinking the latter part could be easily solved with a COUNT(*) and appropriate GROUP BY.)

    Example of data:

    StudentID FunctionName Variables

    1 Example1 Var1=10&Var2=xy&Sign=True&Role=False

  • Thanks for example of data.

    Example of expected result?

    Catch-all queries done right [/url]
    Gail Shaw's Performance Blog[/url]

  • I have to split a string with '&' separated values without the use of a function or stored proc

    Why can't you use a function? There's a great splitter function that it's very well tested and documented, you could try to replicate the functionality without the function, but that's just getting the possibility of breaking it.

    DelimitedSplit8k[/url]

    Example:

    CREATE TABLE #SampleData(

    StudentID int,

    FunctionName varchar(20),

    Variables varchar(8000)

    );

    INSERT INTO #SampleData

    VALUES( 1, 'Example1', 'Var1=10&Var2=xy&Sign=True&Role=False');

    SELECT v.variable,

    COUNT(*) AS varcount

    FROM #SampleData d

    CROSS APPLY dbo.DelimitedSplit8K( d.Variables, '&') s

    CROSS APPLY (SELECT LEFT( s.Item, CHARINDEX( '=', s.Item + '=') - 1)) v(variable)

    GROUP BY v.variable;

    GO

    DROP TABLE #SampleData;

    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

Viewing 3 posts - 1 through 2 (of 2 total)

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