Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-03-29 : 08:51:41
|
| Roshan writes "I have a table called Artists . One of the field in the artist table is "Artist_name".Assume these the values of the fieldsArtist_Id Artist_name1 Andy the coolest2 Coolest3 Cood4 Cool5 CoolerI want to search the artist table where artist name like 'cool'. So logically it will list all the records except artist_id 3 , but I want the output to be sorted based on the search criteria, so my ouput should beArtist_Id Artist_name4 Cool5 Cooler2 Coolest1 Andy the coolestCan you suggest me this output in a single query" |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2006-03-29 : 08:57:11
|
| select the fields you want, use like in the where clause and use order by.. |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-29 : 10:37:58
|
RickD - what did you plan to order by?Roshan - you can use the 'difference' function in your ordering (which works for your example, at least). If the difference function isn't sufficient for evaluating how good matches are, look into full-text searching, or write your own evaluation function.  --datadeclare @Artists table (Artist_Id int, Artist_name varchar(50))insert @Artists select 1, 'Andy the coolest'union all select 2, 'Coolest'union all select 3, 'Cood'union all select 4, 'Cool'union all select 5, 'Cooler'--calculationselect * from @Artists where Artist_name like '%cool%' order by DIFFERENCE(Artist_name, 'cool') desc Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-03-29 : 11:24:35
|
haven't tested this so give it a go.select * from @Artists where Artist_name like '%cool%' order by case when Artist_name like 'cool%' then 1 when Artist_name like '%cool%' then 2 when Artist_name like '%cool' then 3 end Go with the flow & have fun! Else fight the flow |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-29 : 11:42:07
|
quote: Originally posted by spirit1 haven't tested this so give it a go.
I did, and it didn't work for the example , but I do see where you're going . A small modification 'sorts' it out (for the example at least) - it all depends on the sorting criteria Roshan wants to use, I guess.select * from @Artists where Artist_name like '%cool%' order by case when Artist_name = 'cool' then 0 when Artist_name like 'cool%' then 1 when Artist_name like '%cool%' then 2 when Artist_name like '%cool' then 3 end Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-03-29 : 12:14:36
|
lol Go with the flow & have fun! Else fight the flow |
 |
|
|
|
|
|
|
|