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)
 Order by

Author  Topic 

medtech26
Posting Yak Master

169 Posts

Posted - 2008-10-06 : 20:20:32

I have this problem that is hard for me to describe (as well as search the web) so please bear with me on this one :-)

I'm running a query that bring some IDs, I need the data to come back in the same order as it was requested, here's my query:
SELECT id, title FROM myTable WHERE id IN (5,8,65,32);
So sorted results should appear as the input, i.e. 5,8,65,32

How should I approach this?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-06 : 23:56:16
[code]SELECT id, title FROM myTable
WHERE id IN (5,8,65,32)
ORDER BY CASE WHEN id=5 THEN 1
WHEN id=8 THEN 2
WHEN id=65 THEN 3
WHEN id=32 THEN 4
END[/code]
Go to Top of Page

medtech26
Posting Yak Master

169 Posts

Posted - 2008-10-07 : 11:31:44
Thanks, it works like a charm.
Go to Top of Page
   

- Advertisement -