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
 Transact-SQL (2000)
 user-defined order by

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-04-26 : 07:50:34
Prudhvi writes "How would I go about ordering results using a specified order. Say I have non-unique ids, (23, 12, 98, 3) and I need to return rows that contain these ids in the order I specified above. How could I do this without using any extra queries or sub-queries or insert/retrieve from temp tables"

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-04-26 : 08:47:34
You can do:
...ORDER BY CASE id WHEN 23 THEN 1 WHEN 12 THEN 2 WHEN 98 THEN 3 WHEN 3 THEN 4 ELSE 5 END, id

rockmoose
Go to Top of Page
   

- Advertisement -