Solved

Split multiple entries in a column

Posted on 2014-01-21
4
270 Views
Last Modified: 2014-01-22
I have data in a spreadsheet that I have imported to Access 2010.  The data has multiple columns, and in one of them there are multiple entires of names seperated by commas, and slash.  Is there a way in a querry to seperate out those names into rows with keeping all of the other row data attached?
0
Comment
Question by:smurfer1969
  • 3
4 Comments
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39797763
Can you provide a sample of the data in that Names column?  You indicate it is separated by commas and slash, so we would need to see that to get the syntax right.

I would start out with a function that will parse the values of the Name field.
Public Function fnParse(TextToParse As String, Position As Integer, _
                        Optional Delimiter As String = ",") As Variant

    Dim strArray() As String
    
    strArray = Split(TextToParse, Delimiter)
    
    If Position < 1 Or Position > UBound(strArray) + 1 Then
        fnParse = ""
    Else
        fnParse = strArray(Position - 1)
    End If
    
End Function

Open in new window

Then I would create a table (tbl_Numbers) with a single field (intNumber) and numbers to the maximum number of names you have in any given field, plus 1.

You could then create a query that looks something like:

SELECT T.Field1, T.Field2, T.Field3, intNumber, fnParse(T.Names, intNumber, "/") as Name
FROM yourTable as T, tbl_Numbers
WHERE fnParse(T.Names, intNumber, "/") IS NOT NULL

This uses a Cartesian join (no join at all) between these two tables.  which means for every record in yourTable, you would get #n records (the # in tbl_Numbers).  By passing intNumber to fnParse() you are telling it that for this particular record, you want the Nth value from the Names field in YourTable.  And when N exceeds the number of values in your Names field, it will return a NULL, and the WHERE clause will filter those out.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39797765
BTW,

That code assumes that the slash you mentioned in your post separates each name and the name may have a comma to separate the last and first names.
0
 
LVL 7

Accepted Solution

by:
Steve earned 500 total points
ID: 39797839
If you have the extra columns in the database to put it into you might.
If you want to split it in Access you can either run an update query back into the original table or a much safer bet would be to query into a new table and update from there.
You could split the entries with something like:
Mid([Transmissions]![ReportId],1,InStr(1,[Transmissions]![ReportId],".")-1) AS Expr1,

For the first section and...

Mid([Transmissions]![ReportId],InStr(1,[Transmissions]![ReportId],".")+1,InStr(1,[Transmissions]![ReportId],".")-1) AS Expr2
FROM Transmissions;

For the second.
Keep nesting another "InStr" + 1 into the start position of the previous "InStr".

InStr(1,[Transmissions]![ReportId],".")      Becomes:

InStr(InStr(1,[Transmissions]![ReportId],".") +1,[Transmissions]![ReportId],".")

Which makes it start at the next (second) "/". Adding another:

InStr(InStr(InStr(1,[Transmissions]![ReportId],".")  +1,[Transmissions]![ReportId],".") +1,[Transmissions]![ReportId],".")

Makes it start at the 3rd "/" and so on...

 Of course another option is to export it out to Excel, massage it and re-import it.
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 39800870
Good luck with that nested instr concept.

Did you try the solution I provided?  Far more elegant, and provided exactly what you asked for, a single query that provides a separate row with all of the fields the same except for the Name column.

My recommended method of dealing with this would be to assign each of those rows a unique ID and then put the multiple names associated with each row into a separate table with the RowID and the name.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now