dbTalk Databases Forums  

SQL Query help

microsoft.public.sqlserver.misc microsoft.public.sqlserver.misc


Discuss SQL Query help in the microsoft.public.sqlserver.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Andrew
 
Posts: n/a

Default SQL Query help - 07-22-2006 , 04:54 AM






Hi,


I have the following SQL:


select * from tblProducts where prod_name like '%example%' or
prod_description like '%example%'


How do I order the results such that the results found by the prod_name
clause come first, followed by those found by the prod_description clause?

Do i have to union two selects? Is that an efficient way of doing things?


Thanks

Andrew

Reply With Quote
  #2  
Old   
Ed Murphy
 
Posts: n/a

Default Re: SQL Query help - 07-22-2006 , 11:16 AM






On Sat, 22 Jul 2006 09:54:29 GMT, Andrew <infoREMOVE (AT) THISmuonlab (DOT) com>
wrote:

Quote:
I have the following SQL:


select * from tblProducts where prod_name like '%example%' or
prod_description like '%example%'


How do I order the results such that the results found by the prod_name
clause come first, followed by those found by the prod_description clause?

Do i have to union two selects? Is that an efficient way of doing things?
Not sure about efficiency, but here's another approach:

select *
from tblProducts
where prod_name like '%example%'
or prod_description like '%example%'
order by case
when prod_name like '%example%' then 1
when prod_description like '%example%' then 2
else 999 -- can't happen
end


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.