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
 General SQL Server Forums
 New to SQL Server Programming
 SELECT WHERE IN ('value1, 'value2') sort

Author  Topic 

forefj
Starting Member

10 Posts

Posted - 2012-05-14 : 14:30:25
I have a Query with a long list of values (hundreds). I need to have the values returned in the order they are entered into the query,if possible.
For example:
SELECT column
FROM
Table
Where In (Value1,Value2,Value3,Value4,Value5)
I would like for the return values to be displayed in the order they are listed in the query. The standard Order By Column will not work because
the values are returned in alphabetical order, in this case I need them to be returned as listed in the query. Can this be done? If so can you show me how. Thanks.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-05-14 : 14:32:32
ORDER BY CASE WHEN Col10 = 'Value1' THEN 1, WHEN Col10 = 'Value2' THEN 2, ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2012-05-16 : 05:34:03
or store values in a temp table that has identity column. Use join and order by identity column

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-05-16 : 20:55:19
i would prefer temp table solutions especially if number of values involved are large

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -