Split a String

  • Hello Everyone

    I am working on some old data that should not be store this way, but it is.

    My data is two values with pipe separators.

    10.0||14.5

    or

    2||34

    or

    7.1||19

    or

    4||11.7

    I need to query this column and get each value separately. I have no idea how to go about this. There are always double pipe in the middle. But as you can see, there may or may not be decimal values in each. I think that is what is throwing me off. When perhaps, it really may not matter about the actual values.

    I appreciate any and all assistance, suggestions and comments

    Andrew SQLDBA

  • http://www.sqlservercentral.com/articles/Tally+Table/72993/

    by Jeff Moden

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I was actually looking for something else. That really did not help.

    Andrew SQLDBA

  • AndrewSQLDBA (3/12/2013)


    I was actually looking for something else. That really did not help.

    Andrew SQLDBA

    What's the problem with it, Andrew? There is an alternative; I'm just curious.

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • AndrewSQLDBA (3/12/2013)


    I was actually looking for something else. That really did not help.

    Andrew SQLDBA

    Ok, then could you please provide a bit more details about what you have and expected you really want. From the way you have described your problem in the first post it did look like you were after common string split function.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Let me one guess (just a specualtion). Do you want values split to columns? If so, just use the udf like that:

    SELECT *

    INTO #test

    FROM (VALUES ('10.0||14.5'), ('2||34'), ('7.1||19'), ('4||11.7') ) v(val)

    ;

    SELECT val, v1.Item, v2.Item

    FROM #test

    CROSS APPLY (SELECT Item FROM dbo.DelimitedSplit8k(val,'|') WHERE ItemNumber = 1) v1

    CROSS APPLY (SELECT Item FROM dbo.DelimitedSplit8k(val,'|') WHERE ItemNumber = 3) v2

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • For one thing, I do not have a Tally table setup on the server in which I am working.

    So, are there any other ways of doing this without a tally table. There are only about 100,000 rows, and this is a one time update. So I do not really care about performance. What I am most concerned with is the amount of time to have this completed.

    Yes, I can select the data into a temp table, in separate columns there. That would be fine. I can perform the update and then do what I need.

    Thanks in advance

    Andrew SQLDBA

  • AndrewSQLDBA (3/12/2013)


    For one thing, I do not have a Tally table setup on the server in which I am working.

    So, are there any other ways of doing this without a tally table. There are only about 100,000 rows, and this is a one time update. So I do not really care about performance. What I am most concerned with is the amount of time to have this completed.

    Yes, I can select the data into a temp table, in separate columns there. That would be fine. I can perform the update and then do what I need.

    Thanks in advance

    Andrew SQLDBA

    Actually, you don't need a tally table to run the DelimitedSplit8K function. It uses a dynamic tally table built in the function.

  • AndrewSQLDBA (3/12/2013)


    Hello Everyone

    I am working on some old data that should not be store this way, but it is.

    My data is two values with pipe separators.

    10.0||14.5

    or

    2||34

    or

    7.1||19

    or

    4||11.7

    I need to query this column and get each value separately. I have no idea how to go about this. There are always double pipe in the middle. But as you can see, there may or may not be decimal values in each. I think that is what is throwing me off. When perhaps, it really may not matter about the actual values.

    I appreciate any and all assistance, suggestions and comments

    Andrew SQLDBA

    For a one-off update, just use charindex

    SELECT LEFT(value,CHARINDEX('||',value)-1) as Part1,STUFF(value,1,CHARINDEX('||',value)+1,'') as Part2

    MM



    select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);

  • Forum Etiquette: How to post Reporting Services problems
  • [/url]
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • [/url]
  • How to Post Performance Problems - by Gail Shaw
  • [/url]

  • Thank You Mister Magoo

    That worked perfectly

    Thanks for the suggestions everyone, just way overkill for what was actually needed.

    Andrew SQLDBA

  • ChrisM@Work (3/12/2013)


    AndrewSQLDBA (3/12/2013)


    I was actually looking for something else. That really did not help.

    Andrew SQLDBA

    What's the problem with it, Andrew? There is an alternative; I'm just curious.

    the default DelimitedSplit8K is limited to a single char delimiter, which is one tripwire, since he's got dbl pipes for the delimiter. so he'd have to adapt it, or find-and-replace the dbl piple with a single char, and either solution might not be obvious.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Many ways to skin this cat:

    WITH SampleData AS (

    SELECT val=REPLACE(val, '.', '#')

    FROM (VALUES ('10.0||14.5'), ('2||34'), ('7.1||19'), ('4||11.7') ) v(val))

    SELECT C1=REPLACE(PARSENAME(REPLACE(val, '||', '.'), 2), '#', '.')

    ,C2=REPLACE(PARSENAME(REPLACE(val, '||', '.'), 1), '#', '.')

    FROM SampleData;


    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

  • Lowell (3/12/2013)


    ...the default DelimitedSplit8K is limited to a single char delimiter, which is one tripwire, since he's got dbl pipes for the delimiter. so he'd have to adapt it, or find-and-replace the dbl piple with a single char, and either solution might not be obvious.

    That statement is generally true, but in this case he still has just a single pipe character that can be used to split the string with DelimitedSplit8K. So no replaces necessary at all...

    [EDIT: I see Eugene posted basically the same thing above. Great minds think alike! πŸ™‚ ]

    --just to keep the examples simple

    --I'm using a temp table for the sample data

    IF OBJECT_ID('tempdb..#TempTable') IS NOT NULL

    DROP TABLE #TempTable

    CREATE TABLE #TempTable (

    [Val] NVARCHAR(50) NULL)

    INSERT INTO #TempTable

    SELECT '10.0||14.5' UNION ALL

    SELECT '2||34' UNION ALL

    SELECT '7.1||19' UNION ALL

    SELECT '4||11.7'

    If you just need all the values in a single column how simple can it get?

    DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?

    SELECT

    dsk.Item

    FROM

    #TempTable AS sd

    CROSS APPLY

    dbo.DelimitedSplit8K(sd.val,'|') AS dsk

    WHERE

    ItemNumber IN (1,3)

    /*

    Item

    10.0

    14.5

    2

    34

    7.1

    19

    4

    11.7

    */

    And if you need to keep the pairs in their own columns just add another CROSS APPLY.

    Still no replaces or string manipulation is necessary...DelimitedSplit8K will do its magic!

    SELECT

    dsk1.Item AS FirstVal

    ,dsk2.Item AS SecondVal

    FROM

    #TempTable AS sd

    CROSS APPLY

    dbo.DelimitedSplit8K(sd.val,'|') AS dsk1

    CROSS APPLY

    dbo.DelimitedSplit8K(sd.val,'|') AS dsk2

    WHERE

    dsk1.ItemNumber = 1

    AND dsk2.ItemNumber = 3

    /*

    FirstValSecondVal

    10.014.5

    234

    7.119

    411.7

    */

  • Steven Willis (3/13/2013)


    DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?

    +1 πŸ˜‰

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • ChrisM@Work (3/14/2013)


    Steven Willis (3/13/2013)


    DelimitedSplit8K is far from overkill. Why use a hammer when you have a nail gun?

    +1 πŸ˜‰

    Well, if you only have a couple dozen nails to drive and you don't have the correct size in a strip or coil that fits your nail gun and your air compressor is in the garage buried under six lawn chairs, two coolers, a weed eater, and a couple of pairs of roller blades and you'd need to borrow or buy an additional 50' of air hose to reach your work area, the hammer may be the better choice!

    The OP mentioned that he only needed to do a one-time job on about 100,000 rows. It seems much more expedient to write the CHARINDEX code and hit "execute" than to create the function and then write a query that calls it.

    On the other hand, if I didn't already HAVE the nail gun and compressor, a project that involves driving a few dozen nails would make a pretty good justification for acquiring them! πŸ˜€ The OP might be well-served in the future by having the DelimitedSplit8K function ready to go when he is tasked with a tougher string-split operation, i.e., the time it would take to create it now will likely pay off in the future if there is much string-splitting to be done in his shop..

    Jason Wolfkill

  • Viewing 15 posts - 1 through 14 (of 14 total)

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