• Daniel Matthee (8/2/2014)


    So this can easily be done in SQL.

    But since this is string parsing it will be intense on the sql server. So i recommend to rather change the way the data are received.

    But if you want to attempt in doing this in sql

    here you go:

    declare @String varchar(50) = 'CHILD=1625=0n=1==3&ADULT=1900=1j=1==1'

    /*

    -- Get the text left of the first =

    select left(@String,charindex('=',@String,1)-1)

    --get the first index of the &

    select charindex('&',@String)

    -- get the first index of the first = after the first &

    select charindex('=',@String,charindex('&',@String))

    -- Get the text between the first & and only look at the amount of char to return.

    -- First & index + 1The amount of char to return

    select SUBSTRING(@String,charindex('&',@String)+1, charindex('=',@String,charindex('&',@String))-charindex('&',@String)-1)

    */

    select left(@String,charindex('=',@String,1)-1)+', '+ SUBSTRING(@String,charindex('&',@String)+1,charindex('=',@String,charindex('&',@String))-charindex('&',@String)-1)

    I have added some small select statements so you can see what each step is trying to do.

    Hope this helps

    This is much better than the function in the previous comment. That function is a multi table valued function, which are almost as bad as UDFs. The optimiser will treat it like a black box.