string splitting with charindex

  • Hi

    i have the following field in a progress db which contains analysis codes for a given account. I need to split the codes into their own columns.

    here's an example: 007;007;01D;P;999;999;

    I am using the following to get the position of the ; character and then using substing to try and pull out the parts. The code is becoming painful already though and i'm only at the 3rd code.

    Is there an easier way to do this?

    charindex(';', analcode) pos1,

    charindex(';', analcode, charindex(';',analcode)+1) pos2,

    charindex(';', analcode, charindex(';', analcode, charindex(';',analcode)+1)+1) pos3

    Thanks

  • spin (11/7/2013)


    Hi

    i have the following field in a progress db which contains analysis codes for a given account. I need to split the codes into their own columns.

    here's an example: 007;007;01D;P;999;999;

    I am using the following to get the position of the ; character and then using substing to try and pull out the parts. The code is becoming painful already though and i'm only at the 3rd code.

    Is there an easier way to do this?

    charindex(';', analcode) pos1,

    charindex(';', analcode, charindex(';',analcode)+1) pos2,

    charindex(';', analcode, charindex(';', analcode, charindex(';',analcode)+1)+1) pos3

    Thanks

    Take a look at the link in my signature about splitting strings. It will show a super easy and fast way to do this.

    Also, I might suggest changing the name of your column. 😛

    _______________________________________________________________

    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/

  • thanks, i'll check that out.

    believe me, our anal codes have been the humour in many a meeting. 🙂

  • Also, I might suggest changing the name of your column.

    :w00t:

    Reminds me of a report one of my ex co-workers used to run every month.

    It was a pay analysis and it was called payanaly. A disturbing concept.

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Phil Parkin (11/7/2013)


    Also, I might suggest changing the name of your column.

    :w00t:

    Reminds me of a report one of my ex co-workers used to run every month.

    It was a pay analysis and it was called payanaly. A disturbing concept.

    BWAHAHAHA!

    I had a similar thought. 😛


    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 5 posts - 1 through 4 (of 4 total)

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