Unix Technical Forum

Query sort question

This is a discussion on Query sort question within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a field called "type" in my "school" table that can have three possibilities: Elementary Middle High In ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 05:42 AM
Ralph Noble
 
Posts: n/a
Default Query sort question

I have a field called "type" in my "school" table that can have three
possibilities:

Elementary
Middle
High

In my result set, I always want Elementary to come first, Middle to
come second, and High to come third. An alpha sort gets me this:

Elementary
High
Middle

Sure, I could create a look-up that assigns an integer to the "type"
field to get the right sort order (i.e. elementary = 0, middle = 1,
high = 2) ... but is there a better way to do this with SQL syntax?

Thanks,

Ralph Noble
ralph_noble@hotmail.com
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 05:42 AM
Hugo Kornelis
 
Posts: n/a
Default Re: Query sort question

On 1 Nov 2004 11:23:53 -0800, Ralph Noble wrote:

>In my result set, I always want Elementary to come first, Middle to
>come second, and High to come third.

(...)
> ... but is there a better way to do this with SQL syntax?


Hi Ralph,

ORDER BY CASE Type
WHEN 'Elementary' THEN 1
WHEN 'Middle' THEN 2
WHEN 'High' THEN 3
ELSE NULL
END

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:34 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com