• 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
    New Generation Software

    Free NGS-IQ Webinar: A Better Way to Connect IBM i Data and Microsoft 365 Users.

    Business users rely on Excel, SharePoint, Access, and other Microsoft tools to view, analyze, and share data. But what if your data is too sensitive and complex to simply open up to non-technical users via ODBC and Microsoft 365’s query functions?

    Attend our webinar on June 22, 2022, and see how you can make the people you support smile even as you centrally manage and secure IBM i data access.

    www.ngsi.com– 800-824-1220

    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

  • POWERUp Brings IBM i Base Back Together in the Big Easy
  • New Nav for i Brings New Stuff to You
  • Why Infor’s IDF Is Important for Customer Innovation
  • Four Hundred Monitor, May 25
  • IBM i PTF Guide, Volume 24, Number 21
  • How Committed Is Big Blue To The IBM Cloud?
  • Immutable Copies Are Only As Good As Your Validation
  • Guru: IBM i *USRPRF Security
  • ERP Transitions Loom for SAP on IBM i Customers
  • Inflation Pumps Up Global IT Spending, Supply Chain Deflates It

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 © 2022 IT Jungle

loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.