Sql 在access查询中拆分姓、名和中间名
我有一个全名字段,我想将其拆分并从名称中删除中间名 名称如下: 史密斯,詹姆斯D->结果是:史密斯,詹姆斯 Doe,John Snow->结果是:Doe,John 以下是我所做的,但不确定删除中间名时遗漏了什么Sql 在access查询中拆分姓、名和中间名,sql,ms-access,ms-access-2010,Sql,Ms Access,Ms Access 2010,我有一个全名字段,我想将其拆分并从名称中删除中间名 名称如下: 史密斯,詹姆斯D->结果是:史密斯,詹姆斯 Doe,John Snow->结果是:Doe,John 以下是我所做的,但不确定删除中间名时遗漏了什么 FName: Mid([Employee] & "",InStr(1,[Employee] & " ",",")+1) Lname: Left([Employee] & "",InStr(1,[Employee] & "",",")+(InStr(1,[E
FName: Mid([Employee] & "",InStr(1,[Employee] & " ",",")+1)
Lname: Left([Employee] & "",InStr(1,[Employee] & "",",")+(InStr(1,[Employee] & "",",")>0))
史密斯,詹姆斯D->我得到->FName:詹姆斯D
多伊,约翰·斯诺->我明白->名字:约翰·斯诺
结构的一致性在字符串操作中至关重要。假设每个名称始终有3个且只有3个部分,第3个部分是中间名/首字母,逗号后没有空格,请考虑: LastFirst:左[员工],仪表[员工],-1 最后:左[员工],仪表[员工],-1 第一个:左中[Employee],仪表板[Employee],-1,仪表板[Employee],+1 中间:中间[员工],仪表[员工],+1 如果任何假设不成立,则构建VBA自定义函数。例如,不允许中间的首字母/名称:
Function GetNamePart(strPart As String, strName As String) As Variant
Select Case StrPart
Case "Last"
GetNamePart = Left(strName, InStr(strName,",")-1)
Case "First"
If InStrRev(strName, " ") > 0 Then
GetNamePart = Mid(Left([Employee], InstrRev([Employee]," ")-1), Instr([Employee],",")+1)
Else
GetNamePart = Mid(strName, InStr(strName, ",")+1)
End If
Case "Middle"
If InStrRev(strName, " ") > 0 Then
GetNamePart = Mid([Employee], InStrRev([Employee], " ")+1)
Else
GetNamePart = Null
End If
End Select
End Function
从查询或文本框调用该函数,例如:GetNamePartLast,[Employee]
与假设的差异越大,代码就越复杂。如果变化足够大,这几乎不可能实现自动化
构造此代码的方法不止一种。数组或集合对象可能很有用。在字符串操作中,结构的一致性至关重要。假设每个名称始终有3个且只有3个部分,第3个部分是中间名/首字母,逗号后没有空格,请考虑: LastFirst:左[员工],仪表[员工],-1 最后:左[员工],仪表[员工],-1 第一个:左中[Employee],仪表板[Employee],-1,仪表板[Employee],+1 中间:中间[员工],仪表[员工],+1 如果任何假设不成立,则构建VBA自定义函数。例如,不允许中间的首字母/名称:
Function GetNamePart(strPart As String, strName As String) As Variant
Select Case StrPart
Case "Last"
GetNamePart = Left(strName, InStr(strName,",")-1)
Case "First"
If InStrRev(strName, " ") > 0 Then
GetNamePart = Mid(Left([Employee], InstrRev([Employee]," ")-1), Instr([Employee],",")+1)
Else
GetNamePart = Mid(strName, InStr(strName, ",")+1)
End If
Case "Middle"
If InStrRev(strName, " ") > 0 Then
GetNamePart = Mid([Employee], InStrRev([Employee], " ")+1)
Else
GetNamePart = Null
End If
End Select
End Function
从查询或文本框调用该函数,例如:GetNamePartLast,[Employee]
与假设的差异越大,代码就越复杂。如果变化足够大,这几乎不可能实现自动化
构造此代码的方法不止一种。数组或集合对象可能很有用。以下简单函数删除中间名并仅返回最后一个、第一个,即使FullName中没有中间名或有多个中间名: 公共函数FixNamefullName作为字符串 最后变暗为字符串 先调暗为弦 last=SplitfullName, FixName=last0 first=Splitlast1, FixName=FixName&、&first0 端函数
以下简单函数删除中间名并仅返回Last、First,即使FullName中没有中间名或有多个中间名: 公共函数FixNamefullName作为字符串 最后变暗为字符串 先调暗为弦 last=SplitfullName, FixName=last0 first=Splitlast1, FixName=FixName&、&first0 端函数
还没有测试过,但这对你来说不管用吗?在vba公共函数中
'First word being first name
FName= split([employee], ",")(0)
'Second word being the last name. This will throw `indexOutOfRange` error if employee name is only one word. Use `on error resume next` to silently ignore?
LName = split(replace([employee], ",", " "))(1)
Public function FnSplit(text as variant, index as integer, optional d as string = " ") as string
If nz(len(text),0) = 0 then exit function
On error resume next
FnSplit = split(text,d)(index)
End function
您不能在ms access sql中直接使用拆分,但这不会阻止您创建自定义拆分函数
'First word being first name
FName= split([employee], ",")(0)
'Second word being the last name. This will throw `indexOutOfRange` error if employee name is only one word. Use `on error resume next` to silently ignore?
LName = split(replace([employee], ",", " "))(1)
Public function FnSplit(text as variant, index as integer, optional d as string = " ") as string
If nz(len(text),0) = 0 then exit function
On error resume next
FnSplit = split(text,d)(index)
End function
并在sql中使用
Select
FnSplit([employee],0) as fName,
FnSplit(replace([employee], ",", " "),1) as lName
From
Your table
显然,这些只是做与前面答案相同的工作的另一种方式,没有测试过,但这对你来说不管用吗?在vba公共函数中
'First word being first name
FName= split([employee], ",")(0)
'Second word being the last name. This will throw `indexOutOfRange` error if employee name is only one word. Use `on error resume next` to silently ignore?
LName = split(replace([employee], ",", " "))(1)
Public function FnSplit(text as variant, index as integer, optional d as string = " ") as string
If nz(len(text),0) = 0 then exit function
On error resume next
FnSplit = split(text,d)(index)
End function
您不能在ms access sql中直接使用拆分,但这不会阻止您创建自定义拆分函数
'First word being first name
FName= split([employee], ",")(0)
'Second word being the last name. This will throw `indexOutOfRange` error if employee name is only one word. Use `on error resume next` to silently ignore?
LName = split(replace([employee], ",", " "))(1)
Public function FnSplit(text as variant, index as integer, optional d as string = " ") as string
If nz(len(text),0) = 0 then exit function
On error resume next
FnSplit = split(text,d)(index)
End function
并在sql中使用
Select
FnSplit([employee],0) as fName,
FnSplit(replace([employee], ",", " "),1) as lName
From
Your table
显然,这些只是做与前面答案相同工作的另一种方式谢谢!回答得好。但有些人没有中间名,我也要考虑一下。请看修改后的答案。谢谢!回答得好。但有些人没有中间名,我也必须考虑这个问题。请看修订答案。