Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

string splitting with charindex Expand / Collapse
Author
Message
Posted Thursday, November 7, 2013 9:00 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 14, 2014 8:25 AM
Points: 116, Visits: 402
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
Post #1512323
Posted Thursday, November 7, 2013 9:07 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:19 AM
Points: 13,064, Visits: 11,897
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1512326
Posted Thursday, November 7, 2013 9:13 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, April 14, 2014 8:25 AM
Points: 116, Visits: 402
thanks, i'll check that out.

believe me, our anal codes have been the humour in many a meeting.
Post #1512330
Posted Thursday, November 7, 2013 9:17 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: 2 days ago @ 11:49 PM
Points: 4,973, Visits: 11,660
Also, I might suggest changing the name of your column.




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.



Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

When you ask a question (and please do ask a question: "My T-SQL does not work" just doesn't cut it), please provide enough information for us to understand its context.
Post #1512338
Posted Thursday, November 7, 2013 9:10 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:12 PM
Points: 3,609, Visits: 5,220
Phil Parkin (11/7/2013)
Also, I might suggest changing the name of your column.




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!

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?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1512523
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse