Skip to content
Advertisement

Get best hit first

I have a table department with a columns SearchKeysName and BOLD_ID. Bold_ID is just a number for identification of row. It contains data like this:

To search data I have SQL like this:

In this case I searched for 32-1 so it will pick the rows above. What I want is to make sure the exact hit is placed on top of the result. Like this

This is of course a simplified example. In reality there are thousands of rows so I can miss the first line with 32-1 as there are many others that and with this string when search string is short.

Only idea I have so far is to make 2 searches. One with

If that don’t find anything then try more generic like above.

EDIT Now tried this:

And got this

Advertisement

Answer

Use a case expression to get exact hit rows first:

To get distinct BOLD_ID’s, do a GROUP BY, to sort a BOLD_ID with an exact match first (even if it also has a non-exact match):

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement