Jump to content

[SQL] Split Field Strings at ;

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Hey CC'ers,

Once again I got a question. I'm still working with reporting services.

My problem is that i have a table in a database and in that table I've got a row with a column name Results.
Well in my Result field I've the following information yes;no;no;yes;yes
Now I want to split those Results into seperate Rows.

So that I will have the following:

Name | Answer
-----------------
Result1 | yes
Result2 | no
Result3 | no
Result4 | yes
Result5 | yes

Instead of

Name | Answer
-----------------
Result1 | yes;no;no;yes;yes


I hope someone understand my problem and has a solution for this matter :P.


Thanks In Advance!!
//SterAllures
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#2
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
I read a few articles and I think it has somenthing to do with SUBSTRING to select the part you want to trim. And maybe somenthing like CHARINDEX to find the value behind ;. So I hope somebody can help me how to write the correct query

Edited by SterAllures, 26 May 2009 - 08:18 AM.

4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Substring is used for strings not databases. I do not think there is a way to make it give you results that look like this:

Name | Answer
-----------------
Result1 | yes;no;no;yes;yes

Since result1 isnt true on all of them - maybe you could show us a bit more details.

#4
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Ya I Think I Explained It Kinda Wrong But I Got A Part Of The Answer Already Here It Is!

Table: SPLITSTRINGS


ColumnNames ->       Results	|   Result1   |   Result2   |

		    ----------------------------------------------------

Values->	     Ja;nee;no	|NULL          | NULL          |	


Query:
UPDATE SPLITSTRINGS

Set Result1 = SUBSTRING(Results, CHARINDEX(‘;’, Results) +1, LEN(Results))

WHERE Result1 LIKE ‘%;%’



The Result

ColumnNames ->       Results	|   Result1   |   Result2   |

		    ----------------------------------------------------

Values->	     Ja;nee;no	|nee;no      | NULL          |


So Now I But The answers from column Results into seperate Columns.

If I Now Edit the query A Bit I will get the following:

ColumnNames ->       Results	|   Result1   |   Result2   |

		    ----------------------------------------------------

Values->	     Ja;nee;no	|nee;no      | no         |


But my Question Now Is. How can I edit the query so that I will only get the last value of the Results Columns into a seperate one?

Edited by SterAllures, 26 May 2009 - 10:28 PM.

4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
You want it automatically just take the last one? hmm that would absolutely be alot harder.
is it always the same amount of results? if so, you could to it as you do now recursively for the fifth.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Ya I think I got it kind of wrong again :P.

I want to split a field which is the same amount every time (5 answers) yes or no seperated by ;.

So I would want somenthing like.

if you have a table with name, adult, male, work, drunk, sports
(somenthing like that)

now I want to seperate the already insert data into those fields so it has to split in at those fields so like:

ColumnNames ->       John	|   adult   |   male   |    work      |  drunk   |   sports
		    ---------------------------------------------------------------------
Values->	     	                |      no      |     yes     |    yes       |     no     |   yes

I really hope you understand it :P:P!
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
Well, why not just use your query, but use ot bothways from the start and from the end.
put the part before the ; in one field and the thing after the ; in another field, rerun the query and so on, this is best to do on a temporary table, maybe within a stored procedure.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
mm I will look into that stored procedure and try to edit the query, Thanks for your time :D, hope it works than ;). is there a special subject of stored procedure I could look into?
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#9
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
A stored procedure is more or less a stored set of sql queries, with some loops, conditions and variables to help.
is this something you need to do often, or seldom? wher ewill you use the results from this? in another programming language? then it will probably be easier to do this division inside that language instead of in SQL.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#10
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
well I use it in reporting services and that's used with VB.NET but I think its easier to do it in SQL cause I will need it more and if it has top be coded in VB.NET you have to edit teh code everytime to make it suitable for the software and for SQL you don't have to do that and just add the function to every database so I think it is easier that way :P
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"

#11
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
another question I forgot to ask; do you have control of the storage of the information, and could that be altered to a better solution?
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#12
SterAllures

SterAllures

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 203 posts
Well I do have the control over the storage at this moment but I didn't before and that's where it went wrong, I now have to fix that. the information is in the database but in the wrong way so that's need to be fixed :P
4d 65 6c 76 69 6e 0d 0a
"If happiness was the national currency, what kind of work would make you rich?"