import string with array into table

  • Hello again,

    I am running into a problem importing a string containing an array into a table.

    My string looks like '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    I want to import this into a temp table that looks like:

    ID, Fname, Lname, Sex

    1, mark, peters, mr

    2, jane, fonda, mrs

    3, john,doo,mr

    4, james,bond,mr

    Can someone help me with this? I really have no clue how to solve this.

    Thanks a lot!

    Mike

  • There are any number of solutions to this issue. Search for "comma delimited strings" in SQLServerCentral and you will get plenty of examples of functions that will do just as you need.

  • Thanks jmcgarvey,

    The problem for me is not the comma delimetered insertion. For me the problem is to add multiple records with multiple columns into a table.

    Hope that you, or someone else, can help me with this...

    Thanks again

    Mike

  • Here's what I came up with using the DelimitedSplit function refernced in my signature:

    declare @val varchar(8000)

    set @val= '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]';

    WITH cte AS

    -- separate the input values into "rows" and eliminate the leading and trailing bracket.

    (

    SELECT

    REPLACE(Item,'{','') as row,

    ItemNumber as pos,

    ROW_NUMBER() OVER (ORDER BY ItemNumber DESC) AS r

    FROM dbo.DelimitedSplit8K(REPLACE(REPLACE(@val,'[',''),']',''),'}')

    ), cte_splittedValues AS

    -- separate the input values into "columns" excluding the last "row" that would only be an empty string

    (

    SELECT x.ItemNumber, x.Item, cte.pos, cte.r

    FROM cte

    CROSS APPLY

    (

    SELECT *

    FROM dbo.DelimitedSplit8K(cte.row,',')

    )x

    WHERE cte.r >1

    )

    -- use the "old-fashioned" Cross-Tab approach to get the table backt together

    SELECT

    Pos AS Id,

    MAX(CASE WHEN ItemNumber = 1 THEN Item ELSE NULL END) AS Fname,

    MAX(CASE WHEN ItemNumber = 2 THEN Item ELSE NULL END) AS Lname,

    MAX(CASE WHEN ItemNumber = 3 THEN Item ELSE NULL END) AS Sex

    FROM cte_splittedValues

    Group by Pos

    ORDER BY Pos



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • You can also use PatternSplitCM (note the link in my signature)....

    declare @val varchar(8000)= '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]';

    WITH x AS

    (

    SELECT ItemNumber, Item

    FROM dbo.PatternSplitCM(REPLACE(REPLACE(@val,'[{',''),'}]',''),'%[}{]%')

    WHERE Matched=0

    )

    SELECTROW_NUMBER() OVER (ORDER BY x.ItemNumber) AS Id,

    MAX(CASE WHEN ps.ItemNumber=1 THEN ps.Item END) AS Fname,

    MAX(CASE WHEN ps.ItemNumber=3 THEN ps.Item END) AS Lname,

    MAX(CASE WHEN ps.ItemNumber=5 THEN ps.Item END) AS Sex

    FROM x

    CROSS APPLY dbo.PatternSplitCM(x.item,',') ps

    Group by x.ItemNumber

    ORDER BY x.ItemNumber;

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Alan.B (10/7/2013)


    You can also use PatternSplitCM (note the link in my signature)....

    declare @val varchar(8000)= '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]';

    WITH x AS

    (

    SELECT ItemNumber, Item

    FROM dbo.PatternSplitCM(REPLACE(REPLACE(@val,'[{',''),'}]',''),'%[}{]%')

    WHERE Matched=0

    )

    SELECTROW_NUMBER() OVER (ORDER BY x.ItemNumber) AS Id,

    MAX(CASE WHEN ps.ItemNumber=1 THEN ps.Item END) AS Fname,

    MAX(CASE WHEN ps.ItemNumber=3 THEN ps.Item END) AS Lname,

    MAX(CASE WHEN ps.ItemNumber=5 THEN ps.Item END) AS Sex

    FROM x

    CROSS APPLY dbo.PatternSplitCM(x.item,',') ps

    Group by x.ItemNumber

    ORDER BY x.ItemNumber;

    Exactly what I was thinking when I saw the problem statement!


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • But it can also be done with a single call to PatternSplitCM:

    WITH SampleData(MyString) AS

    (

    SELECT' [{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    )

    SELECT MyString

    ,Col1=MAX(CASE WHEN ItemNumber%6 = 2 THEN Item END)

    ,Col2=MAX(CASE WHEN ItemNumber%6 = 4 THEN Item END)

    ,Col3=MAX(CASE WHEN ItemNumber%6 = 0 THEN Item END)

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b

    WHERE Matched=1

    GROUP BY MyString, (ItemNumber-1)/6;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Hello all,

    Thanks for your help. The solutions you've provided are super and far beyond my current skill level.

    Learned a lot, Thanks again!!!

    Mike

  • Mike Saunders NL (10/8/2013)


    Hello all,

    Thanks for your help. The solutions you've provided are super and far beyond my current skill level.

    Learned a lot, Thanks again!!!

    Mike

    Not sure who said this but it applies in your case:

    "Everything is difficult ... until it is easy."

    -- Anonymous??

    Glad to help out.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (10/7/2013)


    But it can also be done with a single call to PatternSplitCM:

    WITH SampleData(MyString) AS

    (

    SELECT' [{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    )

    SELECT MyString

    ,Col1=MAX(CASE WHEN ItemNumber%6 = 2 THEN Item END)

    ,Col2=MAX(CASE WHEN ItemNumber%6 = 4 THEN Item END)

    ,Col3=MAX(CASE WHEN ItemNumber%6 = 0 THEN Item END)

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b

    WHERE Matched=1

    GROUP BY MyString, (ItemNumber-1)/6;

    Is there anything PatternSplitCM CAN'T do? :Wow:

  • erikd (10/9/2013)


    dwain.c (10/7/2013)


    But it can also be done with a single call to PatternSplitCM:

    WITH SampleData(MyString) AS

    (

    SELECT' [{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    )

    SELECT MyString

    ,Col1=MAX(CASE WHEN ItemNumber%6 = 2 THEN Item END)

    ,Col2=MAX(CASE WHEN ItemNumber%6 = 4 THEN Item END)

    ,Col3=MAX(CASE WHEN ItemNumber%6 = 0 THEN Item END)

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b

    WHERE Matched=1

    GROUP BY MyString, (ItemNumber-1)/6;

    Is there anything PatternSplitCM CAN'T do? :Wow:

    It can't cook waffles or launch pork chops very well. 😛

    _______________________________________________________________

    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/

  • Sean Lange (10/9/2013)


    erikd (10/9/2013)


    dwain.c (10/7/2013)


    But it can also be done with a single call to PatternSplitCM:

    WITH SampleData(MyString) AS

    (

    SELECT' [{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    )

    SELECT MyString

    ,Col1=MAX(CASE WHEN ItemNumber%6 = 2 THEN Item END)

    ,Col2=MAX(CASE WHEN ItemNumber%6 = 4 THEN Item END)

    ,Col3=MAX(CASE WHEN ItemNumber%6 = 0 THEN Item END)

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b

    WHERE Matched=1

    GROUP BY MyString, (ItemNumber-1)/6;

    Is there anything PatternSplitCM CAN'T do? :Wow:

    It can't cook waffles or launch pork chops very well. 😛

    If you'd like to post your create table statements and some sample data along with some butter and maple syrup I'd be happy to help you with that. :hehe:

  • Mike Saunders NL (10/7/2013)


    Hello again,

    I am running into a problem importing a string containing an array into a table.

    My string looks like '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    I want to import this into a temp table that looks like:

    ID, Fname, Lname, Sex

    1, mark, peters, mr

    2, jane, fonda, mrs

    3, john,doo,mr

    4, james,bond,mr

    Can someone help me with this? I really have no clue how to solve this.

    Thanks a lot!

    Mike

    Mike,

    Will there always be exactly 2 commas between each set of curly braces?

    --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)

  • Sean Lange (10/9/2013)


    erikd (10/9/2013)


    dwain.c (10/7/2013)


    But it can also be done with a single call to PatternSplitCM:

    WITH SampleData(MyString) AS

    (

    SELECT' [{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    )

    SELECT MyString

    ,Col1=MAX(CASE WHEN ItemNumber%6 = 2 THEN Item END)

    ,Col2=MAX(CASE WHEN ItemNumber%6 = 4 THEN Item END)

    ,Col3=MAX(CASE WHEN ItemNumber%6 = 0 THEN Item END)

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(MyString, '[a-zA-Z]') b

    WHERE Matched=1

    GROUP BY MyString, (ItemNumber-1)/6;

    Is there anything PatternSplitCM CAN'T do? :Wow:

    It can't cook waffles or launch pork chops very well. 😛

    BWAHAHAHAH!

    And it can't split comma delimited strings as fast as DelimitedSplit8K.

    However I do like to think there are many creative uses for it:

    http://www.sqlservercentral.com/Forums/FindPost1503344.aspx


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Jeff Moden (10/9/2013)


    Mike Saunders NL (10/7/2013)


    Hello again,

    I am running into a problem importing a string containing an array into a table.

    My string looks like '[{mark,peters,mr}{jane,fonda,mrs}{john,doo,mr}{james,bond,mr}]'

    I want to import this into a temp table that looks like:

    ID, Fname, Lname, Sex

    1, mark, peters, mr

    2, jane, fonda, mrs

    3, john,doo,mr

    4, james,bond,mr

    Can someone help me with this? I really have no clue how to solve this.

    Thanks a lot!

    Mike

    Mike,

    Will there always be exactly 2 commas between each set of curly braces?

    I sense a solution on after-burners in the air.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 15 posts - 1 through 15 (of 17 total)

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