sql - Search sorting inSQL -


i have 4 columns (lastname, firstname, title, city) in table (mssql2008). want display search results in way if search word matches in full should come first other results column. example if have following table

lastname    firstname   title   city kirk        yang        sales rep   seattle fuller      andrew      vice pred   tacoma leverling   janet       sales rep   kirkland peacock     margaret    sales rep   redmond jacob       kirk        sales mag   london suyama      michael     sales rep   london king        robert      sales rep   london callahan    laura       sales cord  seattle david       kirkpatrk   sales rep   london 

and if search kirk result should be:

    lastname    firstname   title   city     kirk        yang        sales rep   seattle             jacob       kirk        sales mag   london     leverling   janet       sales rep   kirkland     david       kirkpatrk   sales rep   london 

edit: using union select results (naive, guess), tired case unable desired result

let me assume using like search , not full text search.

if so, can as:

order ((case when lastname = 'kirk' 1 else 0 end) +           (case when firstname = 'kirk' 1 else 0 end) +           (case when title = 'kirk' 1 else 0 end) +           (case when city = 'kirk' 1 else 0 end)          ) desc,          ((case when lastname '%kirk%' 1 else 0 end) +           (case when firstname '%kirk%' 1 else 0 end) +           (case when title '%kirk%' 1 else 0 end) +           (case when city '%kirk%' 1 else 0 end)          ) desc 

this little more ask for. returns rows exact matches first. returns rows partial matches first.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -