Solved

Split multiple entries in a column

Posted on 2014-01-21
4
273 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager is not included in Office 2013. This comes as a shock to users upgrading from earlier versions of Office, such as 2007 and 2010, where Picture Manager was included as a standard application. This article explains how…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

821 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