检查表中的字段是否有空格或逗号 - MS-Access

发布于 2024-10-07 05:36:18 字数 1750 浏览 8 评论 0原文

我有一个名为 FinalForgotten 的表,其中仅包含一个名为 aname 的字段。该字段可以看起来像 Smith John 或 Smith,John。因此姓氏和名字都在同一字段中,并以空格或逗号分隔。防御字段包含三个字段:first_name、last_name、middle_initial。 first_name 字段将包含与name 字段中的数据完全匹配的数据(例如John)。并且last_name 字段将包含与aname 字段中的一条数据完全匹配的数据(例如Smith)。我试图将所有带有中间首字母的 FinalForgotten 名称记录放入新表中(例如 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 包含一个没有逗号的字段,它将返回“无效的过程调用”,例如: 史密斯·约翰.

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

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。

感谢您的回复。

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 space 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄兵必败 2024-10-14 05:36:18

貌似少了好几个括号。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文