0

我有一个名为 FinalForgotten 的表,它只包含一个名为 aname 的字段。该字段可能看起来像 Smith John 或 Smith,John。因此,姓氏和名字都在同一个字段中,并由空格或逗号分隔。防御字段包含三个字段:first_name、last_name、middle_initial。first_name 字段将包含与 aname 字段中的一个数据(例如 John)完全匹配的数据。而last_name 字段将包含与aname 字段(例如Smith)中的一条数据完全匹配的数据。我正在尝试将所有带有中间首字母的 FinalForgotten aname 记录放入一个新表中(例如,Smith、John S)。防御表就是有这个中间首字母的东西。

这会起作用:

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));

但是如果 FinalForgotten 包含一个没有逗号的字段,它将返回“无效的过程调用”,例如:Smith John。

因此,为了解决这个问题,我尝试考虑逗号是否在该字段中:

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);

这给了我一个“缺少运算符语法”错误,并突出显示了单词 AS。

感谢您的回复。

4

1 回答 1

0

似乎有很多缺少括号。

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
于 2010-12-13T14:34:22.110 回答