Monday, 26 August 2013

SQL Query sort by closest match

SQL Query sort by closest match

We have a Locations search page that is giving us a challenge I've never
run across before.
In our database, we have a list of cities, states, etc. with the
corresponding gecodes. All was working fun until now...
We have two locations in a city named "Black River Falls, WI" and we've
recently opened onein "River Falls, WI".
So our table has records as follows:
Location City State
-------------------------------------
1 Black River Falls WI
2 Black River Falls WI
3 River Falls WI
Obviously our query uses a "LIKE" clause to match city, but when a
customer searches the text "River Falls", in the search results, the first
results shown are always "Black River Falls".
In our application, we always use the first match, and use it as the
default. (We could change it, but it would be a lot of un-budgeted work)
I know I could simple change the sort order to have "River Falls" come up
first, but that's a sloppy solution that works only in this one case.
What I'm wondering is if there is a way, through T-STL (SQL Server 2008r2)
to sort by "best match" where "River Falls" would "win" if we search for
"River Falls, WI" and "Black River Falls" would work if we search for
"Black River Falls" WI.

No comments:

Post a Comment