• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Unusual But Logical SQL Sorting

    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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Rocket Software

    Unlock the full potential of your data with Rocket Software. Our scalable solutions deliver AI-driven insights, seamless integration, and advanced compliance tools to transform your business. Discover how you can simplify data management, boost efficiency, and drive informed decisions.

    Learn more today.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    Avnet Creates Training Portal for Resellers of IBM Wares Redirecting a List of Qshell Commands

    Leave a Reply Cancel reply

Volume 6, Number 33 -- September 13, 2006
THIS ISSUE SPONSORED BY:

WorksRight Software
iTera
Patrick Townsend & Associates

Table of Contents

  • Developers Don’t Despair: Virtualize Your PC
  • Unusual But Logical SQL Sorting
  • Admin Alert: One Common Cure for SQL0901 Package Errors

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle