Newsletters   Subscriptions  Forums  Store   Career  Media Kit  About Us  Contact  Search   Home 
fhg
Volume 4, Number 38 -- November 10, 2004

More Conditional Sorting with SQL

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

Sponsored By
WORKSRIGHT SOFTWARE

Do you need area code information?
Do you need ZIP Code information?
Do you need ZIP+4 information?
Do you need city name information?
Do you need county information?
Do you need a nearest dealer locator system?

We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

Just call us and we'll arrange for 30 days FREE use of either
ZIP/CITY or PER/ZIP4.

WorksRight Software, Inc.
Phone: 601-856-8337
Fax: 601-856-9432
E-mail: software@worksright.com
Web site: www.worksright.com


Technical Editors: Howard Arner, Joe Hertvik, Ted Holt,
Shannon O'Donnell, Kevin Vandever
Managing Editor: Shannon Pastore
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, 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.


THIS ISSUE
SPONSORED BY:

Linoma Software
ProData Computer Svcs
WorksRight Software


BACK ISSUES

TABLE OF
CONTENTS
Executing Dynamic Calculations with Embedded SQL

Let Me Out of Here!

More Conditional Sorting with SQL


The Four Hundred
i5 Model 595: Big Bang for Big Bucks

IBM's New Customer Design Center Focuses on High Availability

Gartner Releases IT and Business Trends Through 2010

Four Hundred Stuff
Unleash the Borg: OS/400 Gets Autonomic Tooling

TeamQuest Brings Capacity Planning Tool to OS/400 Server

No More Coding for EAI? DAM Right, Says Magic

Four Hundred Monitor


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc. (formerly Midrange Server), 50 Park Terrace East, Suite 8F, New York, NY 10034
Privacy Statement