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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with Order By Please....?

Author  Topic 

JoJoDan
Starting Member

3 Posts

Posted - 2010-02-11 : 11:24:41
Hi,

I am working on a search on Member ScreenNames and the query below does what I need with one exception, I would like the results to start with the actual search phrase and then list the results that contain the search phrase in them.

So for this: dbo.Search_SearchMembers 'br'

The result currently are this:
aBreezssey
Brian
Brob
ZdBrdts

The Results I would like are this:
Brian
Brob
aBreezssey
ZdBrdts

How do I do that? I would appreciate any help I could get because I am stumped.

Here is my Original Query:

Alter Procedure dbo.Search_SearchMembers

@SearchTerm varchar(255)

As

Set NoCount On

Set @SearchTerm = '%' + @SearchTerm + '%';

With OrderedResults As
(
Select Row_Number() Over(Order By displayname) As 'Row', *
From dbo.members
Where isguest = 0
And confirmemail = 1
And displayname like @SearchTerm
)

Select *
From OrderedResults


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 11:29:03
[code]order by case when yourstringcolumn LIKE @SearchTerm + '%' THEN 1 ELSE 0 END DESC,yourstringcolumn[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

JoJoDan
Starting Member

3 Posts

Posted - 2010-02-11 : 12:16:33
Awesome thank you!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-11 : 12:19:25
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -