Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Beginner
Oct 24, 2001

Advanced Sorting Using the CASE Statement


Many developers use the CASE statement in the select list of a SQL statement. But not everybody knows that it can be used to do so much more. One example is to use it for advanced sorting.

For example, say that you want to create a guest list for a customer party your company is having. You want the list ordered alphabetically by the names of the people coming. The problem is that some of the contacts are owners, or board members or something, and they need to be at the top of the list (but still sorted alphabetically). The following SQL statement would achieve this:
 
SELECT ContactName, ContactTitle, CompanyName FROM 
Customers
ORDER BY CASE ContactTitle
           WHEN 'Owner' THEN 1
           ELSE 2
         END, ContactName ASC
Christoffer Hedgate
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date