Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: [SQL] Split Field Strings at ;

  1. #1
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    [SQL] Split Field Strings at ;

    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 .


    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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: [SQL] Split Field Strings at ;

    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
    Last edited by SterAllures; 05-26-2009 at 09:18 AM.
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  4. #3
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: [SQL] Split Field Strings at ;

    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:

    Code:
    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.

  5. #4
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: [SQL] Split Field Strings at ;

    Ya I Think I Explained It Kinda Wrong But I Got A Part Of The Answer Already Here It Is!

    Table: SPLITSTRINGS


    Code:
    ColumnNames ->       Results	|   Result1   |   Result2   |
    		    ----------------------------------------------------
    Values->	     Ja;nee;no	|NULL          | NULL          |

    Query:
    Code:
    UPDATE SPLITSTRINGS
    Set Result1 = SUBSTRING(Results, CHARINDEX(‘;’, Results) +1, LEN(Results))
    WHERE Result1 LIKE ‘%;%’


    The Result

    Code:
    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:

    Code:
    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?
    Last edited by SterAllures; 05-26-2009 at 11:28 PM.
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  6. #5
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: [SQL] Split Field Strings at ;

    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

  7. #6
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: [SQL] Split Field Strings at ;

    Ya I think I got it kind of wrong again .

    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:

    Code:
    ColumnNames ->       John	|   adult   |   male   |    work      |  drunk   |   sports
    		    ---------------------------------------------------------------------
    Values->	     	                |      no      |     yes     |    yes       |     no     |   yes
    I really hope you understand it !
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

  8. #7
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: [SQL] Split Field Strings at ;

    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

  9. #8
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: [SQL] Split Field Strings at ;

    mm I will look into that stored procedure and try to edit the query, Thanks for your time , 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?"

  10. #9
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: [SQL] Split Field Strings at ;

    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

  11. #10
    SterAllures's Avatar
    SterAllures is offline Programming Professional
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    203
    Rep Power
    12

    Re: [SQL] Split Field Strings at ;

    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
    4d 65 6c 76 69 6e 0d 0a
    "If happiness was the national currency, what kind of work would make you rich?"

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Split a text field from database in pages
    By constantinone in forum PHP Development
    Replies: 3
    Last Post: 04-18-2011, 10:26 PM
  2. JavaScript Split String Help
    By stevie754 in forum JavaScript and CSS
    Replies: 2
    Last Post: 03-18-2011, 05:34 PM
  3. Wep Page Getting Split Up
    By supermandx in forum PHP Development
    Replies: 2
    Last Post: 09-11-2010, 12:24 PM
  4. Replies: 1
    Last Post: 03-18-2010, 10:15 AM
  5. Split Archive tar.gz?
    By Crop in forum Linux/Unix General
    Replies: 3
    Last Post: 01-15-2009, 07:33 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts