For a search function, I want to return three types of results, so I query three times. It would probably be better to JOIN or UNION the queries into one, but how..
string sql1 = "SELECT DISTINCT area, CHARINDEX(@ss, area) FROM rgmaplocation WHERE area LIKE '%' + @ss + '%' ORDER BY CHARINDEX(@ss, area) ";
string sql2 = "SELECT DISTINCT compound, CHARINDEX(@ss, compound) FROM rgmaplocation WHERE compound LIKE '%' + @ss + '%' ORDER BY CHARINDEX(@ss, compound) ";
string sql3 = "SELECT DISTINCT streetname, CHARINDEX(@ss, streetname) FROM rgmaplocation WHERE streetname LIKE '%' + @ss + '%' ORDER BY CHARINDEX(@ss, streetname)";
Mikael Eriksson’s solution is good if you don’t mind having area, compounds and streetname mixed up. If you want to keep them separate, here you go