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)
 IN() question

Author  Topic 

umut
Starting Member

4 Posts

Posted - 2009-09-17 : 03:50:48
Hello

I want to get the rows as in same order how ids' are specified within IN() clause..

For example i do select * from table where ID IN(110,245,93)

What i am in need and expect get the rows in below order

110

245

93

But it doesnt work as how i expected and it just returns those three rows in a random order such as below;

245

93

110

Any help will be appreciated.

Thanks in advance.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-09-17 : 04:33:08
[code]
select *
from table
where ID IN(110,245,93)
order by case ID when 110 then 1 when 245 then 2 when 93 then 3 else 4 end
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-09-17 : 06:59:50
Seems like your order by is alphanumeric....? In that case (quite ugly but still):

ORDER BY CAST(ID as varchar(8))

- Lumbago
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-09-17 : 07:34:57
quote:
Originally posted by Lumbago

Seems like your order by is alphanumeric....? In that case (quite ugly but still):

ORDER BY CAST(ID as varchar(8))

- Lumbago


Hello Lumbago,

why do you think it is alphanumeric?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2009-09-17 : 10:24:44
Hmm, very good question! Seems like I missed the first sentence of umuts post. I basically read that he wanted the items in the order 110, 245, 93 which you will get if you sort alphanumerically...bummer

- Lumbago
Go to Top of Page

umut
Starting Member

4 Posts

Posted - 2009-09-24 : 04:45:02
thank you for your both help.it is appreciated!!
Go to Top of Page
   

- Advertisement -