• 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
    Manta Technologies

    The Leader in IBM i Education!
    Need training on anything i?
    Manta is all you need.

    130 courses and competency exams on:
    · IBM i operations
    · System Management and Security
    · IBM i Programming Tools
    · Programming in RPG, COBOL, CL, Java
    · Web Development

    SQL, DB2, QueryProduct features:
    · Runs in every popular browser
    · Available 24/7/365
    · Free Student Reference Guides
    · Free Student Administration
    · Concurrent User License
    · Built-In IBM i Simulator

    You can download our 200-page catalog and take sample sessions at MantaTech.com

    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

  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32
  • You Can Now Get IBM Tech Support For VS Code For i
  • Price Cut On Power S1012 Mini Since Power S1112 Ain’t Coming Until 2026
  • IBM i: Pro and Con
  • As I See It: Disruption
  • IBM i PTF Guide, Volume 27, Number 30

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