Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Help with special sort order wanted

 
   Database Help (Home) -> Programming RSS
Next:  Unintended line breaks in text being emailed.  
Author Message
staeri

External


Since: Jan 20, 2008
Posts: 15



(Msg. 1) Posted: Wed Mar 04, 2009 1:13 am
Post subject: Help with special sort order wanted
Archived from groups: microsoft>public>sqlserver>programming (more info?)

I have the following sp:

CREATE PROCEDURE [dbo].[spUserGet]
@UserID nvarchar(50)
AS
BEGIN

SELECT UserID,
UserName
FROM vwUser
ORDER BY UserName

END

I need help with changing the sort order so that the UserName attached
to @UserID always is shown first and then the rest of the users sorted
by UserName. How can this be done?

Best regards,

S

 >> Stay informed about: Help with special sort order wanted 
Back to top
Login to vote
Paddy Mullaney

External


Since: Mar 04, 2009
Posts: 1



(Msg. 2) Posted: Wed Mar 04, 2009 1:40 am
Post subject: Re: Help with special sort order wanted [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

One solutions is to use a union, like so;



CREATE PROCEDURE [dbo].[spUserGet]
@UserID nvarchar(50)
AS
BEGIN
SELECT UserID,
UserName from
(

SELECT UserID,
UserName , 0 as SortOrder
FROM vwUser
where userid = @userid

union

SELECT UserID,
UserName, 1 as SortOrder
FROM vwUser
where userid != @userid

)t
ORDER BY SortOrder, UserName
END

(untested)

Paddy

 >> Stay informed about: Help with special sort order wanted 
Back to top
Login to vote
ML

External


Since: Jan 15, 2008
Posts: 380



(Msg. 3) Posted: Wed Mar 04, 2009 1:47 am
Post subject: RE: Help with special sort order wanted [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Perhaps (untested):

select DerivedTable.UserName
from (
select @UserName as UserName
,0 as HiddenOrderColumn
union -- union all maybe? look it up in Books Online
select UserName
,1
from vwUser
) DerivedTable
order by DerivedTable.HiddenOrderColumn
,DerivedTable.UserName


ML

---
Matija Lah, SQL Server MVP
http://milambda.blogspot.com/
 >> Stay informed about: Help with special sort order wanted 
Back to top
Login to vote
Uri Dimant

External


Since: Aug 24, 2003
Posts: 1744



(Msg. 4) Posted: Wed Mar 04, 2009 5:25 am
Post subject: Re: Help with special sort order wanted [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi

SELECT * FROM Users ORDER BY CASE WHEN UserID=@userid THEN '0' ELSE UserID
END






wrote in message

>I have the following sp:
>
> CREATE PROCEDURE [dbo].[spUserGet]
> @UserID nvarchar(50)
> AS
> BEGIN
>
> SELECT UserID,
> UserName
> FROM vwUser
> ORDER BY UserName
>
> END
>
> I need help with changing the sort order so that the UserName attached
> to @UserID always is shown first and then the rest of the users sorted
> by UserName. How can this be done?
>
> Best regards,
>
> S
 >> Stay informed about: Help with special sort order wanted 
Back to top
Login to vote
staeri

External


Since: Jan 20, 2008
Posts: 15



(Msg. 5) Posted: Wed Mar 04, 2009 5:30 am
Post subject: Re: Help with special sort order wanted [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 4 Mar, 10:51, "Uri Dimant" wrote:
> Hi
>
> SELECT * FROM Users ORDER BY CASE WHEN UserID=@userid THEN '0' ELSE UserID
> END
>
> wrote in message
>
>
>
>
>
> >I have the following sp:
>
> > CREATE PROCEDURE [dbo].[spUserGet]
> > @UserID nvarchar(50)
> > AS
> > BEGIN
>
> > SELECT UserID,
> > UserName
> > FROM vwUser
> > ORDER BY UserName
>
> > END
>
> > I need help with changing the sort order so that the UserName attached
> > to @UserID always is shown first and then the rest of the users sorted
> > by UserName. How can this be done?
>
> > Best regards,
>
> > S- Dölj citerad text -
>
> - Visa citerad text -

Thanks a lot! It works great!

// S
 >> Stay informed about: Help with special sort order wanted 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Help with distinct + sort order - Hi I've the following set of un-normalised records. SortID Customer 1 ccc 2 ccc 3 aaa 4 aaa 5 bbb 6 bbb I need to perform a select which will return distinct Customer name while ..

Sort order and view - HI all, Using SQL 2005, I create a simple view Select * from employee order by name But when I select the view with select * from employeeview the sort order is not adheered to. I have to specificcally issue a order by on the view Can someone explain...

Sort order in SQL 2005 - In a 2005 database table function that has SELECT TOP 100 Percent .... ORDER BY TextField This displays it in the primary key (numeric field) order and not the specified. Is there a setting change that is required to make the function return the order as...

Sort order in SQL 2005? - Hi, When sorting a numeric field in SQL 2005, I am finding that it is sorting it differently than it was in 2000. i.e. 1, 10, 1000, 1001, 1002, 2, 20, 2000, 2002. Is there a reason for this? Appreciate the help. Damon

Wanted: Part-Time Ad hoc SQL Writer - Guys, We are looking for someone that understands SQL Server very well and can write performant queries of complex data. We will supply the database backup, the requirements and give the writer a relatively free hand to suggest DBMS configurations,..
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]