fhg
Volume 12, Number 24 -- October 3, 2012

Alternatives To SQL Literals

Published: October 3, 2012

by Ted Holt

Literals have caused me grief on more than one occasion. Trying to figure out what a certain number or character literal means in a program has wasted too much of my time, and my time is more valuable to me than money. I have written about this topic before, but I have not talked about literals in SQL.

The example I've chosen may not be the best one, since the ratio of pounds to kilograms never changes. I chose it because it's one most people should be able to relate to. The article I just referred to covers the arguments for and against unchanging literals, so I won't rehash them here.

Suppose we work for a factory in the United States that ships products to other countries. The factory uses customary American units of measurement, but documents that go to other countries require metric measurements.

Here's an item master table to start our example.

create table mylib/items
  (item char(7),
   description char(15),
   weight dec(7,2),
   primary key (item))

insert into mylib/items
values ('ABC1000', 'Widget 6-inch', 1.2),
       ('ABC1010', 'Widget 12-inch', 2.4),
       ('ABC1020', 'Widget 18-inch', 3.6),
       ('BR-549', 'Samples', 280)

The weight column refers to pounds. To convert pounds to kilograms is a matter of simple division.

select i.item, i.description, i.weight, 
       dec(round(i.weight / 2.2046,3),7,3) as ShipWeight
  from mylib/items as i

ITEM

ITEM

DESCRIPTION

WEIGHT

SHIPWEIGHT

ABC1000

Widget 6-inch

1.20

0.544

ABC1010

Widget 12-inch

2.40

1.089

ABC1020

Widget 18-inch

3.60

1.633

BR-549

Samples

280.00

127.007


If you wish to replace the literal 2.2046 with something self-documenting, here's one method.

create function mylib/LbsPerKG ()
returns double
language sql
return 2.2046

The LbsToKG (pounds to kilograms) function returns the literal value it replaces. The division looks like this:

select i.item, i.description, i.weight, 
       dec(round(i.weight / LbsPerKG(),3),7,3) as ShipWeight
  from items as i

A second method is to write a function that does the conversion. (I prefer this one to the last one.) Here's a function that converts pounds to kilograms.

create function mylib/LbsToKG ( inPounds double)
returns double
language sql
return inPounds / 2.2046

And here's the rewritten query.

select i.item, i.description, i.weight, 
       dec(round(LbsToKG(i.weight),3),7,3) as ShipWeight
  from items as i

Another technique you might find helpful is to use global variables. For more information, read Michael Sansoterra's excellent article New in DB2 for i 7.1: Use Global Variables to Track Environment Settings.

If you have a will to remove a literal from SQL queries, there is more than one way.


RELATED STORIES

New in DB2 for i 7.1: Use Global Variables to Track Environment Settings

Use Named Constants to Write Clearer Code



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


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


Senior Technical Editor: Ted Holt
Technical Editor: Joe Hertvik
Contributing Technical Editors: Edwin Earley, Brian Kelly, Michael Sansoterra
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.

Sponsored Links

Sirius Computer Solutions:  A comprehensive, cost-effective cloud solution for IBM i users
Tributary Systems:  Storage Director® makes your tape work better. FREE Webinar and PDF
System i Developer:  RPG & DB2 Summit, Oct 23-25 in Minneapolis. Register by Oct 12 to save $100!


 

IT Jungle Store Top Book Picks

BACK IN STOCK: Easy Steps to Internet Programming for System i: List Price, $49.95

The iSeries Express Web Implementer's Guide: List Price, $49.95
The iSeries Pocket Database Guide: List Price, $59
The iSeries Pocket SQL Guide: List Price, $59
The iSeries Pocket WebFacing Primer: List Price, $39
Migrating to WebSphere Express for iSeries: List Price, $49
Getting Started with WebSphere Express for iSeries: List Price, $49
The All-Everything Operating System: List Price, $35
The Best Joomla! Tutorial Ever!: List Price, $19.95


 
The Four Hundred
Some Things To Ponder On The Impending Power7+ Era

iBelieve NY: If You Don't Like Change. . .

Arming The IBM i Nation

Mad Dog 21/21: Shamoon And Six Trends

IBM's Rometty Takes Over As Chairman Of The Board

Four Hundred Stuff
Coglin Mill Reaches Out to Salesforce.com with ETL Connector

Infor Rolls Out Cloud for Heavy Equipment Biz

Android or iOS: Which Mobile OS Fits Best with IBM i?

Remain Software Adds to Multi-Platform Choices

Joomla Now Optimized for Mobile Devices

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

System i PTF Guide
September 29, 2012: Volume 14, Number 39

September 22, 2012: Volume 14, Number 38

September 15, 2012: Volume 14, Number 37

September 8, 2012: Volume 14, Number 36

September 1, 2012: Volume 14, Number 35

August 25, 2012: Volume 14, Number 34

TPM at The Register
Fujitsu, Oracle pair up on future 'Athena' Sparc64 chips

Oracle customers DEMANDED infrastructure cloud

Big mainframe shops embiggen, says BMC survey

Big Blue: 'New PureSystem? Madness? No, THIS IS SPARTA!'

Oracle cranks up the flash with Exadata X3 systems

AMD, Oracle tag-team on GPU acceleration for Java apps

Oracle fudges touts Sparc SuperCluster prowess

OpenStack gets "Folsom" release out on time

Quantcast gives the boot to Hadoop's HDFS

'Double Stuf' Power7+ sockets: Yummy, but so is overclocking

HP pitches four-socketeer Xeon E5 borg boxes

Chambers says Cisco is mulling succession plans

THIS ISSUE SPONSORED BY:

Bug Busters Software Engineering
WorksRight Software
Connectria Hosting


Printer Friendly Version


TABLE OF CONTENTS
Debugging Server Jobs In Green Screen

Alternatives To SQL Literals

Admin Alert: Seven Things You Should Be Monitoring On Your System

Four Hundred Guru

BACK ISSUES




 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2012 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement