I have table called FinalForgotten which only contains one field called aname. The field could either look like Smith John or Smith,John. So both last and first name are in same field and delimited by either spa开发者_如何学Cce or comma. The defense field contains three fields: first_name,last_name,middle_initial. The first_name field will contain data that matches exactly a piece a data IN aname field (e.g. John). And the last_name field will contain data that matches exactly a piece of data IN aname field (e.g. Smith). I'm trying to get all the FinalForgotten aname records with a middle initial into a new table (e.g. Smith,John S). The defense table is what has this middle initial.
This would work:
SELECT left([aname],InStr(1,[aname],",")-1) & " "& right([aname],Len(aname)-InStr(1,[aname],",")) & " "& summary_judgment.middle_initial AS fullnameINTO FinalForgottenWithMiddle FROM FinalForgotten INNER JOIN summary_judgment ON((left(FinalForgotten.aname,InStr(1,FinalForgotten.[aname],",")-1))=summary_judgment.last_name) AND((right(FinalForgotten.aname,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=summary_judgment.first_name));
But it will return "invalid procedure call" should FinalForgotten contain a field that doesn't have a comma like: Smith John.
Hence, to address this, I tried to factor whether a comma was in the field or not:
SELECT left([aname], IIF(instr([aname], ",") = 0, InStr(1,[aname]," ")-1),InStr(1,[aname],",")-1) & ", " & right([aname], IIF(instr([aname], ",") = 0,Len(aname)-InStr(1,[aname]," "),Len(aname)-InStr(1,[aname],",") & " " & defense_final.middle_initial AS fullname INTO FinalForgottenWithMiddle
FROM FinalForgotten INNER JOIN defense_final ON
((right(FinalForgotten.aname,IIF(instr([aname], ",") = 0,Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," ")),Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],","))=defense_final.first_name))
AND
((left(FinalForgotten.aname,,IIF(instr([aname], ",") = 0,InStr(1,FinalForgotten.[aname]," ")-1)),InStr(1,FinalForgotten.[aname],",")-1))=defense_final.last_name);
This gives me a "missing operator syntax" error and highlights the word AS.
Thanks for response.
There seems to be quite a few missing parentheses.
SELECT left(
[aname],
IIF(instr([aname], ",") = 0,
InStr(1,[aname]," ")-1,
InStr(1,[aname],",")-1
)
)
& ", " &
right(
[aname],
IIF(instr([aname], ",") = 0,
Len(aname)-InStr(1,[aname]," "),
Len(aname)-InStr(1,[aname],",")
)
)
& " " &
defense_final.middle_initial AS fullname
INTO FinalForgottenWithMiddle
FROM FinalForgotten
INNER JOIN defense_final
ON
right(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname]," "),
Len(FinalForgotten.aname)-InStr(1,FinalForgotten.[aname],",")
)
)=defense_final.first_name
AND
left(FinalForgotten.aname,
IIF(instr([aname], ",") = 0,
InStr(1,FinalForgotten.[aname]," ")-1,
InStr(1,FinalForgotten.[aname],",")-1
)
)=defense_final.last_name
精彩评论