• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Alternatives To SQL Literals

    October 3, 2012 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

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    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

    Some Things To Ponder On The Impending Power7+ Era Power7+ Launches In Multi-Chassis Power 770+ And 780+ Systems

    Leave a Reply Cancel reply

Volume 12, Number 24 -- October 3, 2012
THIS ISSUE SPONSORED BY:

Bug Busters Software Engineering
WorksRight Software
Connectria Hosting

Table of Contents

  • Debugging Server Jobs In Green Screen
  • Alternatives To SQL Literals
  • Admin Alert: Seven Things You Should Be Monitoring On Your System

Content archive

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

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • 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

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