问题
I have a data in tables as full name. These name contains "Last_Name, First Name Middle Name" I need to fetch first name, last name and middle name from full name and store them in different fields.
For example, I have full names in table as -
Williams, Robert K
Winchester, Sam T
I need to store them as
FirstName Last_Name MName
Robert Williams K
Sam Winchester T
I am doing it on some tables in Ms access. Can anybody please help me how I can achieve this.
回答1:
Here's an attempt at a non-VBA solution:
select
iif(t.name like "*,*", trim(mid(t.name, instr(t.name, ",")+1, len(t.name) - instr(t.name, ",") - iif(t.name like "* ?", 2, 0))), iif(t.name like "* ?", left(t.name, len(t.name)-2), t.name)) as FName,
iif(t.name like "*,*", left(t.name, instr(t.name, ",")-1), Null) as LName,
iif(trim(t.name) like "* ?", right(trim(t.name), 1), Null) as MName
from
YourTable t
Change YourTable
to suit your table name.
Assumptions
The absence of a comma implies that the name is a first name (and possible middle-initial).
Middle initials are assumed to always be 1 character
Examples
Given the sample data:
+--------------------+
| Name |
+--------------------+
| Williams, Robert K |
| Winchester, Sam T |
| Smith, John |
| Jack A |
| Harry |
| ,Thomas T |
+--------------------+
The above query will yield:
+--------+------------+-------+
| FName | LName | MName |
+--------+------------+-------+
| Robert | Williams | K |
| Sam | Winchester | T |
| John | Smith | |
| Jack | | A |
| Harry | | |
| Thomas | | T |
+--------+------------+-------+
回答2:
You can do this, but ONLY if your data is consistant.
What about
Williams, Robert
Or
,Robert
So, was every full name perfectly entered? In my experience this is VERY RARE.
So, the challenge here is not code or a update that splits out all 3 but dealing with data that does NOT have all 3 settings.
The following code should work, and it attempts to deal with missing parts, but how well this code works will much depend on how well and how consistant the full name data is.
So, this code can be placed in a standard code module, and will process each row of data into the fields. You can place your cursor anywhere in the code module, and hit f5, and it will run.
I STRONG (but really STRONG) suggest you test his code on a copy of the database in case it messes your data up beyond repair.
Sub SpltNames()
Dim rstData As DAO.Recordset
Dim strTable As String
Dim vBuf As Variant ' array to hold 3 parts
Dim vBuf2 As Variant ' for first and middle initials
Set rstData = CurrentDb.OpenRecordset("tblCustomers")
Do While rstData.EOF = False
rstData.Edit
' split the 3 parts into the 3 fields.
If InStr(rstData!FullName, ",") = 0 Then
' name has no , so assume just a last name
rstData!Last_name = Trim(rstData!FullName)
Else
' data has a , - split into parts
vBuf = Split(rstData!FullName, ",")
Select Case UBound(vBuf)
Case 0
' no comma, assume just a last name
rstData!Last_name = vBuf(0)
Case 1
' 2 values, like Kallal, Albert, or Kallal, Albert D.
rstData!Last_name = Trim(vBuf(0))
' take second part, and check for space for middle
vBuf2 = Split(Trim(vBuf(1)), " ")
Select Case UBound(vBuf2)
Case 0
' no middle, just take first name
rstData!FirstName = Trim(vBuf2(0))
Case 1
' a space between first and middle, save both
rstData!FirstName = Trim(vBuf2(0))
rstData!MName = Trim(vBuf2(1))
End Select
End Select
End If
rstData.Update ' save data
rstData.MoveNext ' move on to next recod
Loop
rstData.Clone
MsgBox "done processing"
End Sub
来源:https://stackoverflow.com/questions/55345324/fetching-first-name-and-last-name