Microsoft Access Forums

Go Back   Microsoft Access Forums > Access Forums > Queries

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 02-05-2010, 11:35 AM
supraman3001 supraman3001 is offline Windows Vista Access 2007 (version 12.0)
Novice
 
Join Date: Feb 2010
Posts: 5
supraman3001 is on a distinguished road
Default 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!
Reply With Quote
  #2  
Old 02-05-2010, 01:41 PM
NassauBob's Avatar
NassauBob NassauBob is offline Windows XP Access 2003 (version 11.0)
Not THAT Green
 
Join Date: Feb 2010
Location: Coastal North Carolina
Posts: 60
NassauBob is on a distinguished road
Default

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
Reply With Quote
  #3  
Old 02-05-2010, 02:34 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

What do the values in Column2 represent. You may be looking at a normalization issue here.
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #4  
Old 02-05-2010, 03:52 PM
NassauBob's Avatar
NassauBob NassauBob is offline Windows XP Access 2003 (version 11.0)
Not THAT Green
 
Join Date: Feb 2010
Location: Coastal North Carolina
Posts: 60
NassauBob is on a distinguished road
Default

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.
Reply With Quote
  #5  
Old 02-06-2010, 09:35 AM
supraman3001 supraman3001 is offline Windows Vista Access 2007 (version 12.0)
Novice
 
Join Date: Feb 2010
Posts: 5
supraman3001 is on a distinguished road
Default

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!
Reply With Quote
  #6  
Old 02-06-2010, 09:36 AM
supraman3001 supraman3001 is offline Windows Vista Access 2007 (version 12.0)
Novice
 
Join Date: Feb 2010
Posts: 5
supraman3001 is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 02-06-2010, 11:14 AM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

Other than just random numbers, what does column 2 represent? Are they scores or something?
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #8  
Old 02-06-2010, 01:26 PM
supraman3001 supraman3001 is offline Windows Vista Access 2007 (version 12.0)
Novice
 
Join Date: Feb 2010
Posts: 5
supraman3001 is on a distinguished road
Default

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.
Reply With Quote
  #9  
Old 02-06-2010, 01:29 PM
supraman3001 supraman3001 is offline Windows Vista Access 2007 (version 12.0)
Novice
 
Join Date: Feb 2010
Posts: 5
supraman3001 is on a distinguished road
Default

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.
Reply With Quote
  #10  
Old 02-06-2010, 01:57 PM
RuralGuy's Avatar
RuralGuy RuralGuy is online now Windows 7 Access 2007 (version 12.0)
Administrator
 
Join Date: Mar 2007
Location: 8300' in the Colorado Rocky Mountains
Posts: 4,263
RuralGuy will become famous soon enoughRuralGuy will become famous soon enough
Default

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.
__________________
(RG for short) aka Allan Bunch MS Access MVP - WinXP Pro, Win7 - acXP, ac07
If your issue is resolved...follow this link for directions on how to use the Solved thread tool!
Teaching is not filling a bucket but lighting a fire. Borrowed quote..."Docendo discimus"
Reply With Quote
  #11  
Old 02-06-2010, 07:50 PM
NassauBob's Avatar
NassauBob NassauBob is offline Windows XP Access 2003 (version 11.0)
Not THAT Green
 
Join Date: Feb 2010
Location: Coastal North Carolina
Posts: 60
NassauBob is on a distinguished road
Default

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.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Crosstab query for multiple columns hierarchy ysrini Access 1 02-05-2010 05:33 AM
Consolidate multiple columns into one COforlife Access 17 10-19-2009 11:12 AM
Combo Box with multiple columns desireemm1 Access 1 08-17-2009 12:36 AM
Missing Columns in Datasheet View mikel Access 2 08-14-2009 03:48 AM
Multiple Price Columns kmwhitt Access 0 04-04-2009 07:48 PM


All times are GMT -8. The time now is 02:41 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.