|
Question : How do I compare values excluding spaces in MS Access SQL - Part 2
|
|
Hi,
I have read a previous posting on this: http://www.experts-exchange.com/Database/MySQL/Q_21810413.html
This was basically exactly what I need to do as well (although in the middle of a more complex query). I am comparing post codes and telephone numbers in a table to data that a user has input into a form linked to another table. The user input and the data in the table may or may not contain a space in the middle.
The solution suggested was to use REPLACE(TEXT,' ',''). However, I have tried this and I am getting a message saying: "Data type mismatch in criteria expression".
The query worked without using the replace, but would only return a match on the telephone number or postcode if they were entered with the correct spacing. The full query is as follows, the last but one paragraph is the particular part I am asking about:
-------------------------------------------
SELECT DISTINCT c.*
FROM BBH_Customers AS c, Query_Table AS q, (SELECT max(q2.ID) AS Max_Query_ID FROM Query_Table AS q2 WHERE q2.Query_Name='Cust_Search_Query') AS q3
WHERE q.id=q3.Max_Query_ID
And ((LEFT(c.Surname,LEN(q.Data_Input1 & 'x')-1)=q.Data_Input1) Or (q.Data_Input1 Is Null))
And ((LEFT(c.First_Name,LEN(q.Data_Input2 & 'x')-1)=q.Data_Input2) Or (q.Data_Input2 Is Null))
And ((LEFT(IIf(INSTR(NZ(c.Postcode,' x'),' ')<2,c.Postcode,LEFT(c.Postcode,INSTR(NZ(c.Postcode,' x'),' ')-1) & MID(c.Postcode,INSTR(NZ(c.Postcode,' x'),' ')+1,4)),LEN(q.Data_Input3 & 'x')-1)= IIf(INSTR(NZ(q.data_input3,' x'),' ')<2,q.data_input3,LEFT(q.data_input3,INSTR(NZ(q.data_input3,' x'),' ')-1) & MID(q.data_input3,INSTR(NZ(q.data_input3,' x'),' ')+1,4))) Or (q.Data_Input3 Is Null))
And (REPLACE(LEFT(c.Tel_No,LEN(q.Data_Input4 & 'x')-1),' ','') = REPLACE(q.Data_Input4,' ','') Or REPLACE(LEFT(c.Alt_Tel_No,LEN(q.Data_Input4 & 'x')-1),' ','') = REPLACE(q.Data_Input4,' ','') Or (q.Data_Input4 Is Null))
And ((c.Customer_Id=q.Data_Input5) Or (q.Data_Input5 Is Null));
--------------------------------------------------------------
Any ideas would be appreciated.
Thanks,
|
|
Answer : How do I compare values excluding spaces in MS Access SQL - Part 2
|
|
I am assuming you are using LEN(q.Data_Input4 & 'x')-1) to get rid of trailing spaces in c.Alt_Tel_No and c.Tel_No. You can use Trim (c.Alt_Tel_No) and Trim (c.Tel_No) for that. Your data type mismatch may be due to passing a null to Len or Replace. So you can try:
And REPLACE(Trim(Nz(c.Tel_No)),' ','') = REPLACE(Nz(q.Data_Input4),' ','') Or REPLACE(Trim(Nz(c.Alt_Tel_No),' ','') = REPLACE(Nz(q.Data_Input4),' ','') Or (q.Data_Input4 Is Null))
Your undying problem is what is called "GIGO" (Garbage In, Garbage Out) . I would use an update query to either get rid of all spaces in the fields or have all fields have consistent spaces and use the input mask and/or validation rule properties to make sure all future data input is consistent.
|
|
|
|