• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • SQL and Invalid Decimal Data

    August 7, 2013 Ted Holt

    I’m sure no reader of this august publication likes decimal data errors. One single such error can ruin an entire day. The wise programmer uses tools and techniques to keep invalid numeric data from becoming a problem. One such tool that you can use to find and fix invalid data is SQL.

    Assume that a physical file has a customer account number stored as seven digits packed decimal, with no decimal positions. Suppose there are one or more blank records in the physical file. You query the file by customer number, like this:

    select * from baddata
    where customerid = 2026025
    

    The system heartlessly responds with error messages: QRY2283: Selection error involving field CUSTOMERID; and CPD4019: Select or omit error on field BADDATA_1.CUSTOMERID member BADDATA.

    So what’s an i professional to do?

    The solution is to use the HEX function to select the bad records. Since a blank is X’40’ in the EBCDIC collating sequence, this is the SQL command you need.

    select * from baddata
    where hex(customerid) = '40404040'
    

    Once you find the bad data, you can delete it.

    delete   from baddata
    where hex(customerid) = '40404040'
    

    Or, if you prefer, you can correct it.

    update   baddata
    set customerid = 0
    where hex(customerid) = '40404040'
    

    Blanks may be the most common form of invalid decimal data, but they are not the only invalid values. Even so, the HEX function is still the way to find and fix the data. Here’s another example.

    COST is a five-digit, packed-decimal field with two decimal positions. This statement retrieves items that have invalid cost values.

    select i.ItemNumber, i.Description
      from items as i
     where substr(hex(i.cost),1,1) < '0'
        or substr(hex(i.cost),2,1) < '0'
        or substr(hex(i.cost),3,1) < '0'
        or substr(hex(i.cost),4,1) < '0'
        or substr(hex(i.cost),5,1) < '0'
        or substr(hex(i.cost),6,1) not between 'A' and 'F'
    

    This statement will find items with any non-packed item cost, including blanks.

    These illustrations use packed-decimal data. The principle is the same for zoned-decimal data.



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

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Tags:

    Sponsored by
    Lightedge

    Your IBM Power Cloud Strategy Should Start With “Why?”

    Moving IBM i to the cloud isn’t just an infrastructure decision.

    A hardware refresh, data center exit, IBM i skills gap, disaster recovery requirement, or broader cloud initiative can each lead to a very different architecture.

    At Lightedge, we start by understanding what you’re trying to accomplish. Then we look at the workload, application dependencies, resiliency requirements, operating model, and long-term strategy to determine where that workload belongs.

    Managed IBM Power cloud? IBM Power Virtual Server? DR in the cloud? A hybrid architecture connecting IBM Power with AWS or Azure?

    There isn’t one right answer for every IBM i environment.

    Start with the business requirement. Understand the workload. Then decide where it belongs.

    Explore IBM Power cloud, managed services, and hybrid solutions from Lightedge.

    Share this:

    • Share on Reddit (Opens in new window) Reddit
    • Share on Facebook (Opens in new window) Facebook
    • Share on LinkedIn (Opens in new window) LinkedIn
    • Share on X (Opens in new window) X
    • Email a link to a friend (Opens in new window) Email

    Sponsored Links

    Maxava:  FREE Webinar: Test your DR without Downtime. September 12
    System i Developer:  Upgrade your skills at the RPG & DB2 Summit in Minneapolis, Oct 15-17.
    Linoma Software:  GoAnywhere Secure File Transfer. Simplify. Automate. Encrypt.

    More IT Jungle Resources:

    System i PTF Guide: Weekly PTF Updates
    IBM i Events Calendar: National Conferences, Local Events, and Webinars
    Breaking News: News Hot Off The Press
    TPM @ The Reg: More News From ITJ EIC Timothy Prickett Morgan

    IFSPOP–Another (And A Better) IFS Interface Admin Alert: Budgeting For Your Next Power Systems Upgrade

    One thought on “SQL and Invalid Decimal Data”

    • Dan says:
      July 30, 2021 at 10:38 am

      I like this solution since I needed to find and fix a numeric field in one of my customers old S36 file. In my case I don’t want to delete the record I have a Salesman number in the Customer file that I want to run SQL on. However there are records that have Blanks in the numeric field (hex ‘4040’). In the past I wrote an RPG to update the field so I could then use SQL. Then I wondered if I could use SQL to fix the bad records since your solution allows me to find them. Turns out you can if you know how the field is stored.
      In my case the field (INSSLS) is 2.0 Signed field, so I need the hex to be ‘F0F0’.
      So I did this.
      UPDATE qs36f/custmast
      SET INSSLS = X’F0F0′
      WHERE HEX(INSSLS) = ‘4040’

      So by assigning the Hex value of X’F0F0′ I was able to fix the bad records.

      Lets say (INSSLS) is 3.0 Packed field then I would have used X’000F’

      Not going to try and teach how numeric data is stored but knowing that you can use SQL to update a value with Hex is actually a big deal for me. Could be I’m one of the last to know this.

      Reply

    Leave a ReplyCancel reply

Volume 13, Number 15 -- August 7, 2013
THIS ISSUE SPONSORED BY:

PowerTech
WorksRight Software
Profound Logic Software

Table of Contents

  • IFSPOP–Another (And A Better) IFS Interface
  • SQL and Invalid Decimal Data
  • Admin Alert: Budgeting For Your Next Power Systems Upgrade

Content archive

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

Recent Posts

  • IBM i Delivers New Development Tooling With 2026 Fall Tech Refreshes
  • Big Blue Says AI Is Not Behind The Big Uptick In IBM i Security Alerts
  • Guru: Web Service With Pagination
  • The GenAI Revolution And Product Shortages Send IT Budgets Skyward
  • IBM i PTF Guide, Volume 28, Number 40
  • IBM i PTF Guide, Volume 28, Number 41
  • Bob Code Assistant Gets Better Understanding Of IBM i Applications
  • IBM i In The Cloud And The Case For Reversing Replication
  • Guru: From Hiking Boots To Long-Term Memory
  • Inside IBM’s New PowerHA Cloud Offering, Full System Manager

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