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 2000 Forums
 SQL Server Development (2000)
 Order results by paramater

Author  Topic 

sulman
Starting Member

20 Posts

Posted - 2007-03-28 : 05:37:49
Hi,

I need to query my table so that I return all of the rows from it but the selected row first. e.g.:

parameter passed is 3 (id)
result would return:
id title
-- -----
3 Title3
1 Title1
2 Title2
4 Title4
etc...

Could someone please point me in the right direction?

Many thanks
S

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-28 : 05:41:58
select * from table1
order by case when id = @id then 0 else 1 end, id


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-28 : 05:43:33
select id, title from (
select 0 as t, id, title from table1 where id = @id
union all
select 1, id, title from table1 where id <> @id
) as x order by t, id


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

sulman
Starting Member

20 Posts

Posted - 2007-03-28 : 05:57:46
Absolutely brilliant.

Thanks very much!

S
Go to Top of Page

rammohan
Posting Yak Master

212 Posts

Posted - 2007-03-30 : 05:35:34
select * from trail1 where sid = 3 union all select * from trail1 where sid != 3

One can never consent to creep,when one feels an impulse to soar
RAMMOHAN

Go to Top of Page
   

- Advertisement -