Basically I have a table like this:
CREATE TABLE Person(
PersonID int IDENTITY(1,1) NOT NULL,
FirstName nvarchar(512) NOT NULL,
LastName nvarchar(512) NULL
)
And I need to find the top n results based on a user-query like this:
"Joh Smi"
The following query returns the results I need (I think). Just not in the relevant order.
SELECT
PersonID, FirstName, LastName
FROM
Person
WHERE
FirstName LIKE 'Joh%' OR
LastName LIKE 'Joh%' OR
FirstName LIKE 'Smi%' OR
LastName LIKE 'Smi%'
If the following names were in the database and our user-query was "Joh Smi" the names should appear in the following order (or similar)
- John Smith
- Johnny Smith
- John Jacob
- David Smithsonian
- Daniel Johnson
I'm hoping to get it to work similar to facebook's autocomplete friend-search.
So, how do I return the top n most relevant rows in SQL Server 2008?