• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • More Conditional Sorting with SQL

    November 10, 2004 Hey, Ted

    Your tip “Conditional Sorting with SQL” is one cool tip! May I add two more examples?

    My examples are in RPG, but I assume they will work with any language. In both examples, I use host variables to control the sort.

    Host variable SortOption controls the sort in the first example. If host variable SortOption has a value of 1, the data is retrieved in order by customer name. If two customers have the same name, they are retrieved in customer number sequence. If SortOption has any other value, the data is retrieved in customer number sequence.

    D SortOption      S              1P 0
    
    C/exec sql
    C+
    C+   Declare C1 Cursor for
    C+   select cusnum, lstnam, init
    C+   from qiws/qcustcdt
    C+   order by
    C+      case when :SortOption = 1
    C+             then lstnam
    C+             else digits(cusnum) end,
    C+      case when :SortOption = 1
    C+             then cusnum
    C+             else 0 end
    C+
    C/end-exec
    

    In the second example, the data may be sorted by state, in either ascending or descending sequence. If host variable SortSeq has a value of A, the data is returned in ascending order by state. Within state, the data is sorted by last name and initials. A SortSeq value of D causes the states to be sorted in descending order, but the last name and initial fields are sorted in ascending order. If SortSeq has some other value, state is not used for sorting.

    D SortSeq         S              1A
    
    C/exec sql
    C+
    C+   Declare C1 Cursor for
    C+   select state, lstnam, init from qiws/qcustcdt
    C+   order by
    C+      case when :SortSeq = 'A' then state else ' ' end,
    C+      case when :SortSeq = 'D' then state else ' ' end desc,
    C+      lstnam,
    C+      init
    C+
    C/end-exec
    

    –John

    Thanks for the ideas, John. Several readers wrote in to say that they liked the tip, but none of them sent examples.


    Using host variables to affect the behavior of SQL commands is one way to add flexibility to an application. For more information, see these articles from previous editions of this newsletter.

    “Alternate SQL Row-Selection Criteria”

    “Alternate SQL Row-Selection Criteria, Take 2”

    –Ted

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    Raz-Lee Security

    Protect Your IBM i and/or AIX Servers with a Free Virus Scan

    Cyber threats are a reality for every platform, including IBM i and AIX servers. No system is immune, and the best defense is prompt detection and removal of viruses to prevent costly damage. Regulatory standards across industries mandate antivirus protection – ensure your systems are compliant and secure.

    Get My Free Virus Scan

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Symantec Adds Regulatory Compliance to Security Management Tool How the i5s Compare with Other Big Boxes

    Leave a Reply Cancel reply

Content archive

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

Recent Posts

  • POWERUp 2025 –Your Source For IBM i 7.6 Information
  • Maxava Consulting Services Does More Than HA/DR Project Management – A Lot More
  • Guru: Creating An SQL Stored Procedure That Returns A Result Set
  • As I See It: At Any Cost
  • IBM i PTF Guide, Volume 27, Number 19
  • IBM Unveils Manzan, A New Open Source Event Monitor For IBM i
  • Say Goodbye To Downtime: Update Your Database Without Taking Your Business Offline
  • i-Rays Brings Observability To IBM i Performance Problems
  • Another Non-TR “Technology Refresh” Happens With IBM i TR6
  • IBM i PTF Guide, Volume 27, Number 18

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