Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Custom sort order 1

Status
Not open for further replies.

wuxapian

Programmer
Jun 13, 2001
52
0
0
GB
Hi all,

I need to be able to force a query to order records by a certain field in an order I specify.

For example I have records where the descriptions Could be "Cow", "Helicoptor", "Fish" but when I query the database I want to order them by "Fish", "Helicoptor", "Cow". Does anyone know if there is a way of specifying your own sort order?

TIA
Wux.
 
how many animals are we talking about?

ORDER BY
case
when descr = 'Fish' then 1
when descr = 'Helicopter' then 2
when descr = 'Cow' then 3
else 0
end

rudy
 
Thanks Rudy, that was exactly what I needed.

Wux.
 
If you are going to do this with a long list, then it is best to put a SortOrder column in your table and then set the order in that and use an order by statement to get your results to use this column for the order.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top