Comma separated

mac_see

Active Member
Joined
Oct 15, 2002
Messages
419
Hi!

In my table, I have only 1 field (Field1 - data type "Text" - size 38) with thousands of records.

Every record is a combination of 9 numbers separated by comma and then a space.

I want to segregate all numbers and put them in 9 different fields of data type "Byte"

Here is a sample data:

1, 3, 20, 22, 27, 30, 50, 53, 67
1, 4, 6, 10, 20, 23, 31, 48, 59
1, 5, 14, 27, 42, 44, 54, 64, 65
10, 11, 18, 26, 36, 37, 42, 46, 61

Can this be done using a query?

Maxi
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I'm sure you could do it in a query, but when I have to things like that I will bring the data into Excel and use the "text to columns" selection on the data menu.
A wizard will walk you through it, you can seperate by a comma, space among others.

hth
 
Upvote 0
Hi Maxi

Yes you can do this with a couple of queries but be aware that it is very very sloooooooow. VB may be quicker but I'm no good with it. You might want to do this on a speedy PC and definitely don't do this over a network. I tested this with the sample data you provided and whilst it worked ok, it was painfully slow.

You would first need to create a new table with the 9 fields formatted as numbers (plus I included the original csv value also). For the purposes of my append query I used the table name "tblMaxiValues".

Query One
The first query calculates the positions of the comma within the csv field (the POSx columns) and strips out the values based on the positions of the commas (the Valuex columns).

The SQL for the first query follows (please note I used the field name [csv] and the table name tblMaxi - you will need to change these to your actual table and field names) :

SELECT tblMaxi.csv, InStr([csv],",") AS Pos1, [Pos1]+InStr(Right([csv],Len([csv])-[Pos1]),",") AS Pos2, [Pos2]+InStr(Right([csv],Len([csv])-[Pos2]),",") AS Pos3, [Pos3]+InStr(Right([csv],Len([csv])-[Pos3]),",") AS Pos4, [Pos4]+InStr(Right([csv],Len([csv])-[Pos4]),",") AS Pos5, [Pos5]+InStr(Right([csv],Len([csv])-[Pos5]),",") AS Pos6, [Pos6]+InStr(Right([csv],Len([csv])-[Pos6]),",") AS Pos7, [Pos7]+InStr(Right([csv],Len([csv])-[Pos7]),",") AS Pos8, Left([csv],[Pos1]-1) AS Value1, Mid([csv],[Pos1]+2,[Pos2]-[Pos1]-2) AS Value2, Mid([csv],[Pos2]+2,[Pos3]-[Pos2]-2) AS Value3, Mid([csv],[Pos3]+2,[Pos4]-[Pos3]-2) AS Value4, Mid([csv],[Pos4]+2,[Pos5]-[Pos4]-2) AS Value5, Mid([csv],[Pos5]+2,[Pos6]-[Pos5]-2) AS Value6, Mid([csv],[Pos6]+2,[Pos7]-[Pos6]-2) AS Value7, Mid([csv],[Pos7]+2,[Pos8]-[Pos7]-2) AS Value8, Right([csv],Len([csv])-[Pos8]-1) AS Value9
FROM tblMaxi;

I named the query "qryMaxiCommas" - if you save the query as another name then it will impact the SQL for the 2nd query which appends the values from the first query into the new table.

Query Two
The 2nd query appends the values into your new table (I used the variable names 1 through 9 and I included the original csv variable too) I used the table name "tblMaxiValues" - there is probably no reason why you can't create the new fields in the original table and append them back there but I didn't try that. The SQL for the 2nd query follows :

INSERT INTO tblMaxiValues ( csv, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
SELECT qryMaxiCommas.csv, qryMaxiCommas.Value1, qryMaxiCommas.Value2, qryMaxiCommas.Value3, qryMaxiCommas.Value4, qryMaxiCommas.Value5, qryMaxiCommas.Value6, qryMaxiCommas.Value7, qryMaxiCommas.Value8, qryMaxiCommas.Value9
FROM qryMaxiCommas;

If you have any questions or issues with this, please ask.

HTH, Andrew. :)
 
Upvote 0
Phew ! A difficult task. I will do this when I get back home coz I don't have a faster PC here. Will keep you posted on this topic.

I guess a P4 2.8 Ghz with 256 MB RAM is sufficient, will it?

Thanx and Regards,
Maxi
 
Upvote 0
Hi Maxi
I ran it on my test PC (a P2 450 with 256Mb) and it was painful with just the sample 4 records - haven't tried it on the P4 but my guess is the RAM will be the constraint plus you mentioned there were 200k records so it may be an overnight job. You might want to shut down any other programmes before you run it and check your swap file / virtual memory settings and disable hibernation.
A
 
Upvote 0
Is it possible to calculate the sum of the 9 numbers in that string without writing such a big query?

No doubt, this is easily possible after writing that query. I was just wondering if this can be done without the long SQL
 
Upvote 0

Forum statistics

Threads
1,221,849
Messages
6,162,425
Members
451,765
Latest member
craigvan888

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top