[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Split multiple entries in a column

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
smurfer1969
Asked:
smurfer1969
  • 3
1 Solution
 
Dale FyeCommented:
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
 
Dale FyeCommented:
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
 
SteveCommented:
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
 
Dale FyeCommented:
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now