2010-12-13 62 views
0

我有一個名爲FinalForgotten的表,它只包含一個名爲aname的字段。該領域可能看起來像史密斯約翰或史密斯約翰。所以最後和名字都在同一個字段中,並由空格或逗號分隔。防禦字段包含三個字段:first_name,last_name,middle_initial。 first_name字段將包含完全匹配數據IN aname字段(例如John)的數據。 last_name字段將包含與aname字段中的一段數據完全匹配的數據(例如Smith)。我試圖把所有的FinalForgotten aname記錄與一箇中間的首字母變成一張新表(例如Smith,John S)。防守表是這個中間名首字母。檢查表中的字段是否有空格或逗號 - MS-Access

這會工作:

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

這給了我一個「缺少運算符語法」錯誤,並強調作爲字。

感謝您的回覆。

回答

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 
+0

謝謝我也喜歡你佈置查詢的方式。 – JohnMerlino 2010-12-13 15:12:24