I have a database table that contains among other things partial postal codes. I’m trying to write a query that will take a postal code and find the row in the table that matches that code as close as possible. In the example below a Gold customer with postal code ‘A1A B2E’ would match the first row and a Bronze customer with the same postal code would match the third row
CUST_TYPE | POST_CODE | SHIPPING_SURCHARGE
------------------------------------------
Gold | A1A | 0.99
Gold | A2A | 1.01
Gold | A | 3.00
Bronze | A | 1.05
Silver | A | 1.02
Bronze | B | 1.07
In all cases the query would query by both the CUST_TYPE and the POST_CODE columns. I’d want the query to only return a single row containing the one row that best matches the postal code. So, if I query Gold and ‘A1AB2B’ I would want the first row (Gold, A1A, 0.99) to be returned not both the first and third rows
SQL-Server
Oracle
(I can’t test now:)