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)
 Ordering Issue

Author  Topic 

vipinspg
Starting Member

12 Posts

Posted - 2005-07-06 : 08:08:14
Hi All

Suppose we have a table named employee with columns empid and empname. Also we are having a string containing comma separated emp ids. My requirement is we need to fetch employee records in the same order of ids as in the string. For example, if the string contains '2,1,5', then we need to get employee records with empid having 2 should come first, then record with empid 1 and finally the record with empid 5. Can anyone suggest a solution for this ?

Thanks in advance for your time.


Thanks
Vipins

.

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-06 : 08:47:04
[code]select columns from yourtable order by
case when col=2 then 1 when col=1 then 2 when col=5 then 3 end[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-07-06 : 09:19:35
This works when the order is hardcoded.....but I suspect the poster intends the "csv list" to be a parameter, in which case the order is relative to the start of the csv list.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-06 : 09:28:43
In that case I dont think it will be done through dynamic Order by

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

vipinspg
Starting Member

12 Posts

Posted - 2005-07-07 : 03:06:31
Hi madhivanan

Thanks for your answer. This is what I was expecting. Thanks a lot.

Vipins

.
Go to Top of Page
   

- Advertisement -