Results 1 to 11 of 11
  1. #1
    supraman3001 is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Posts
    5

    Splitting a comma seperated value into multiple columns using SQL View

    Splitting a comma seperated value into multiple columns using SQL View
    I have a table that looks something like this:

    Column1 Column2 Column3
    John 12, 34, 5,498 Texas
    Mike 16, 8, 69, 54 New York
    Robert 104, 42, 110 Mexico
    Josh 25 Maine


    I am trying to seperate the values in Column2 into multiple columns that hold single values so that for instance the first row's Column2 holds each value in a different Column:
    Column1 Column2 Column3 Column4
    12 34 5 498

    Basically I am trying to normalize the data and without doing this, I cannot move forward. I would really appreciate if someone can help me solve this using some sort of SQL Query in Access's SQL View.



    Thanks in advance!

  2. #2
    NassauBob's Avatar
    NassauBob is offline Not THAT Green
    Windows XP Access 2003
    Join Date
    Feb 2010
    Location
    Augusta, GA
    Posts
    61
    You are going to have to use a combination of MID() and INSTR() functions, nested, so that they extract the correct substrings from that raw data.

    Example: to get the first numeric portion out, you can use MID([Column2],Instr([Column2],1),Instr([Column2],",")-1)

    I'll explain. the MID() function takes three arguments: string value to be manipulated ([Column2]), starting position (in the case of the FIRST numeric value, it's the first character), and desired length of the substring (in this case, it won't necessarily always be the same, so you set that value to the position just before the separator; the ",").

    Is that clear as mud for ya? If you want a more detailed and thorough explanation, PM me

  3. #3
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    What do the values in Column2 represent. You may be looking at a normalization issue here.

  4. #4
    NassauBob's Avatar
    NassauBob is offline Not THAT Green
    Windows XP Access 2003
    Join Date
    Feb 2010
    Location
    Augusta, GA
    Posts
    61
    I do too. I was looking at his before values, and wondering if he had commas in table cells. Since I saw no commas between name and the first numeric value, nor last numeric and location, I assumed he was separating columns by spaces.

  5. #5
    supraman3001 is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Posts
    5
    Thanks Shane and sorry about the duplicate post Ruralguy.

    To answer your question, Column2 represents all those numbers. As Shane stated, Column1 shows the name, Column2 represents the numbers (which btw are in a "string" data type) and Column3 has the location.

    Shane, I will try what you said first thing Monday morning and let you know how things work out. Any other suggestions would be kindly appreciated.

    Thanks again!

  6. #6
    supraman3001 is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Posts
    5
    Oh btw, Shane...

    Can we use the Instr() in Access 2007 SQL View? I remember getting an error when trying to use that function and also read somewhere that it is not compatible with access.

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Other than just random numbers, what does column 2 represent? Are they scores or something?

  8. #8
    supraman3001 is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Posts
    5
    these will be used as the primary key linked with the other table. The other table has these numbers as well 1-XXXX....each number here in column2 basically needs to be linked to its corresponding number in table2.

  9. #9
    supraman3001 is offline Novice
    Windows Vista Access 2007
    Join Date
    Feb 2010
    Posts
    5
    so for instance.
    John from Texas is linked to ID 12, 34, 5,and 498 in table2.

    There are instances where two different names from table1 are linked to the same ID in table2 as well.

  10. #10
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 Access 2007
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Well I will stop commenting because the design seems very strange. More than ever I feel there is probably a normalization issue that deserves further study and attention. The bottom line is that each time you have to deal with this part of your system, you will end up spending extra time making it work because of the normalization issues. It can certainly be done but it just takes longer.

  11. #11
    NassauBob's Avatar
    NassauBob is offline Not THAT Green
    Windows XP Access 2003
    Join Date
    Feb 2010
    Location
    Augusta, GA
    Posts
    61
    Well let me first say that normalizing your data, which you did say was the reason for this exercise in your first post, is a good idea. As RuralGuy can certainly attest, normalized data will be much easier to bend to your will down the road. But I am preaching to the choir I know, because that is what you said you were doing this for, right? In the opening post, to normalize your data? Cool.

    Now. What you can do is make a query that breaks up that comma-separated group into 4 distinct pseudo-columns in query design view. How do you do that? By setting a column up to extract each numeric portion out at a time.

    I'm going to PM you about this problem: I have something for you to try.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Missing Columns in Datasheet View
    By mikel in forum Access
    Replies: 3
    Last Post: 01-02-2015, 01:57 PM
  2. Replies: 1
    Last Post: 02-05-2010, 08:33 AM
  3. Consolidate multiple columns into one
    By COforlife in forum Access
    Replies: 17
    Last Post: 10-19-2009, 01:12 PM
  4. Combo Box with multiple columns
    By desireemm1 in forum Access
    Replies: 1
    Last Post: 08-17-2009, 02:36 AM
  5. Multiple Price Columns
    By kmwhitt in forum Access
    Replies: 0
    Last Post: 04-04-2009, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums