fhg
Volume 6, Number 33 -- September 13, 2006

Unusual But Logical SQL Sorting

Published: September 13, 2006

Hey, Ted:

A physical file that I've loaded with data from another system has a department field that I would like to use for sorting. The department field contains a description, rather than a code. I need to sort the data on the description, but not in alphabetical order. That is, I want a certain department to appear first, then a certain department to appear next, and so on. The only way I know to pull this off is to create a little table that lists the departments and the order each one appears in the sort, but I do not like to clutter up the database with little one-use files. Do you have another idea?

--Ross


Yes, I do. Let's assume there are three departments--Accounting, Shipping, and Receiving--and you want to sort the data in that order. Alphabetical sorting will put Receiving ahead of Shipping, so that's out.

Give the LOCATE function a try. In the first parameter, specify the name of the sort field. (I'll assume it's DEPT for this example.) The second parameter should contain a list of the departments. If the department field is fixed-length, be sure to pad each department name in the list-including the last one-with trailing blanks. Here's an example:

select * from mydata                                          
 order by locate(dept, 'Accounting  Shipping    Receiving   ')

If you prefer, you can also use the POSITION and POSSTR functions.

select * from mydata                                              
 order by posstr(dept, 'Accounting  Shipping    Receiving   ')    
select * from mydata                                              
 order by position(dept in 'Accounting  Shipping    Receiving   ')

If DEPT is a variable-length field, you don't have to pad with trailing blanks.

select * from mydata2                                 
 order by posstr(dept, 'AccountingShippingReceiving')

Or you can avoid the trailing blanks by dropping trailing blanks from the search argument.

select * from mydata                                      
order by locate(trim(dept), 'AccountingShippingReceiving')

Be aware that these functions return zero if the search argument is not in the list. Therefore, records for any departments you omit from the list will sort at the top of the returned data.

--Ted



Sponsored By
ITERA

HIGH AVAILABILITY CASE STUDY:

 

When the tornados hit,
TheBANK of Edwardsville was prepared.

 

Spring's balmy 70-degree weather brought several tornados to St. Claire County, Illinois. One powerful storm left dozens of businesses and residents without electricity for nearly a week.

 

While thousands of people were caught by surprise, TheBANK of Edwardsville was not.

 

Read the complete story here.



Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

MKS:  Application lifecycle management solutions
California Software:  Migrate iSeries apps to Windows, Linux, or Unix
COMMON:  Join us at the Spring 2007 conference, April 29 – May 3, in Anaheim, California

 


 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement