简体   繁体   中英

MS Access SQL Where Clause with LEFT function

This is in MS ACCESS, I am looking to have the user enter a parameter and return values based on the left 6 characters in the user's input. I should not use the LEFT function in the where clause as it is frowned upon for processing load reasons that are beyond me. I also cannot use LIKE because the user can input any characters into the parameter. The code is:

SELECT Table1.[Customer Name], Table1.[Customer Code], Table1.[Part Number], Table1.Description, Table1.Vehicle,
       IIF ([Enter Part Number] = Table1.[Part Number], "YES, Exact Match", IIF (LEFT([Enter Part Number],6) = LEFT(Table1.[Part Number],6), "Possible Match - Base 6", "NONE")) AS Interchangeability
FROM Table1
WHERE [Customer Name] LIKE "*" & [Enter Customer Name] & "*" 
AND [Part Number] LIKE [Enter Part Number] & "*" ;

The problem is: if the user enters the full part number as the parameter, the query only returns that full part number. I am wanting to return all part numbers that have the Base-6 part number... so where the first six characters are the same. for example, the where clause I want to use is:

WHERE [Customer Name] LIKE "*" & [Enter Customer Name] & "*" 
AND LEFT([Part Number],6) = LEFT([Enter Part Number],6);

I could prompt the user to only input 6 characters, but we also need the full part number for exact match reasons. I could have them enter the base 6 and the full part number, but this is extra work and is not scalable. Please let me know if there is another way to do this or if I must use the frowned-upon LEFT function in the where clause.

You could combine LIKE and LEFT, where LEFT is used only on the right hand side of the Where statement and not on every record you want to check.

WHERE [Customer Name] LIKE "*" & [Enter Customer Name] & "*" 
AND [Part Number] LIKE LEFT([Enter Part Number],6) & "*" ;

Maybe you should measure that.

Be sure the user is not allowed to input wildcard characters, like * or ?.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM