[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Order by 'starts with' then by 'contains'

Posted on 2010-11-11
8
Medium Priority
?
582 Views
Last Modified: 2012-05-10
I have an autocompleter for an ecommerce site where customers can enter text (@usrstr) and the query will return either products that start with that text or product categories that contain that text. The results are sorted alphabetically.  I want the results to sort first by records that start with the user's text then by records that contain that text.
If a customer enters in 'M', then results might look like
Combustion Motor
Damper
M2942-110
M2942-111
Motors
Sump Pumps

Currently Returns:
Combustion Motor
Damper
M2942-110
M2942-111
Motors
Sump Pumps

--Desired Result
M2942-110
M2942-111
Motors
Combustion Motor
Damper
Sump Pumps

-- Current Query
(SELECT TOP 5 itm_num
  FROM item
WHERE itm_num like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')

--Attempt at Desired Result Set
(SELECT TOP 5 itm_num
  FROM item
WHERE itm_num like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like @usrstr + '%')
union
(SELECT DISTINCT TOP 5 ctg_name
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')


In the end the query can only return one column of data.  Any help is appreciated.
0
Comment
Question by:mjmetzger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 16

Accepted Solution

by:
EvilPostIt earned 2000 total points
ID: 34112975
You could try the following..

SELECT SearchTitle FROM
(SELECT TOP 5 itm_num as 'SearchTitle'
  FROM item
WHERE itm_num like @usrstr + '%'
union
SELECT DISTINCT TOP 5 ctg_name as 'SearchTitle'
  FROM category
WHERE ctg_name like '%' + @usrstr + '%')A
ORDER BY CHARINDEX(@usrstr,SearchTitle)
0
 
LVL 18

Expert Comment

by:deighton
ID: 34113000
use a case statement to calculate a field as 0 for first character is 'M' and 1 otherwise

then select all fields containing M and order by your calculated field.

here is an example on one of my tables, here using surname sort as an illustration

note quotes around calculated field 'init' in the sort - I find you need those

SELECT [EmployeeId],    
      [Surname]
      ,CASE SUBSTRING(SURNAME,1,1) WHEN 'M' THEN  0 ELSE 1 END as init
  FROM MyMaster
  WHERE SURNAME LIKE '%M%'
  ORDER BY 'init', surname
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113013
This will also order based upon how close to the begining of the string the original search is. For example if the user searched for M they would get

MXXXXX
XMXXXX
XXMXXX
XXXMXX
XXXXMX
XXXXXM

You you want to further order these results with the matching position you could also do ORDER BY CHARINDEX(@usrstr,SearchTitle),SearchTitle
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:mjmetzger
ID: 34113122
Creative Solution!  
0
 

Author Comment

by:mjmetzger
ID: 34113170
EvilPostIt, what is the purpose of that 'A' after the parenthesis? I thought it was a typo at first, but the query won't run without it.  Aparently the character itself is arbitrary, but i've never seen it before.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113183
You have to alias the table when using subqueries. If you were to do a join there would be no way to refer to that table as it does not have a name.
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 34113198
You can call it anything you like. I just chose A cause i was being last. You could call it BLAHBLAH if you really liked.
0
 

Author Comment

by:mjmetzger
ID: 34113373
perfect. thanks again!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question