fhg
Volume 8, Number 34 -- October 8, 2008

Testing for Valid Data Representation in SQL

Published: October 8, 2008

Hey Mike:

How can I tell if a character column contains a valid representation of numeric data? I have a column expression in a Select statement that needs to convert character Column A to numeric only when it contains a valid numeric representation. If Column A doesn't contain a numeric representation, then I need to convert Column B to numeric.

--FHG reader

Testing for valid numeric representation is an important task. The problem is that casting character data to numeric will result in a run time error if the data is invalid and therefore it is appropriate to test the data before performing a cast. SQL Server's T-SQL and Visual Basic for Applications (VBA) both contain an "IsNumeric" function to test if a string contains data that can be converted to numeric. Unfortunately, DB2 for i has no such built-in function. However, there are a variety of ways to perform this test.

The first approach is to use the powerful translate function:

Select Case When Trim(Translate(Data, '    ', '+-E.0123456789'))> ' '
            Then 'N' Else 'Y' End As IsNumeric
  From DataTable

In this example, the Translate function is applied to column "Data". Any valid numeric characters are replaced by a space. If, after all the numeric characters are removed, there is still something other than a space, then the CASE statement will set this numeric test to "N" (i.e., not numeric). If only spaces are left, then it is assumed that the data is numeric.

This technique is very useful but suffers from the limitation that there is no consideration given to the order of the characters or number of special characters, such as the plus (+) or minus (–) symbols, contained within the string. For instance, the value "32+5E23+.2-" would be considered a valid numeric value with this technique! Further, any embedded spaces or a value of all spaces would pass this test. Clearly much more work is needed to bulletproof this approach.

An alternative approach would be to write a User-Defined Function (UDF) to do this task. And instead of reinventing the wheel with complicated TRANSLATE logic to find if the data is valid, this UDF would simply re-use what IBM has already given us--the ability to cast character data to numeric with error handling. Here is a function definition that emulates SQL Server's IsNumeric function.

Create Function xxxxx/IsNumeric 
(@TestData VarChar(64))
Returns Char(1)
Deterministic
Not Fenced
Language SQL
Set Option Commit=*None, UsrPrf=*Owner 
Begin
 Declare @Double Double Not Null Default 0;
 Declare Exit Handler For SQLException 
  Return 'N'; 
 If @TestData Is Null Then 
  Return 'N';
 End If; 
 Set @Double=Cast(@TestData As Double); 
 Return 'Y';
End

The function receives the character data in variable "@TestData" and attempts to convert it to the DOUBLE numeric data type. DOUBLE was chosen because it can hold numbers ranging from -2.23x10308 to 2.23x10308. (Starting with version 6.1, developers can use the new DECFLOAT data type to get a much greater range of numbers!) If the conversion succeeds, the function returns a Y. If the conversion fails or is NULL, IsNumeric returns an N.

This function can handle the following representations:

Negatives: '-327.35'

Positives: '+267'

Floating point: '-1.7546E-10'

Also, the function is smart enough to know that problems like embedded spaces, or an "E" that is not used as an exponent, constitute an invalid representation.

When used within a Select column expression, IsNumeric can be used as follows:

Case When IsNumeric(Column1)= 'Y'
    Then Cast(Column2 As datatype)
    Else Cast(Column3 As datatype) End

Of course, this assumes that the data types of both casts are compatible. Depending on the situation, it may be better to cast just once:

Cast(Case When IsNumeric(Column1)= 'Y'
        Then Column2
        Else Column3 End As datatype)

It's easy to use SQL to write additional functions such as "IsDate" or "IsTime" to test for a valid date or time representation. Function IsDate is shown here:

Create Function xxxxx/IsDate
(@Date VarChar(10))
Returns Char(1)
Deterministic 
Not Fenced         
Language SQL
Set Option Commit=*None, UsrPrf=*Owner 
Begin
    Declare @TestDate Date;
    Declare Exit Handler For SQLException 
        Return 'N'; 
    If @Date Is Null Then 
        Return 'N';
    End If; 
    Set @TestDate=Cast(@Date As Date);
    Return 'Y';
End;

Dates are a little bit more difficult to work with because DB2 makes an assumption about how the character date should be formatted based on the SQL session's date format and date separator settings. First of all, you're always safe if your date is in one of the four-digit year formats with the proper separator (*ISO,*USA,*EUR,*JIS) regardless of your session's date format setting. However, if you're expecting the character data to be in one of the following formats--*YMD,*MDY,*DMY,*JUL--then your SQL session's date format setting must match. This also means that you can't validate *YMD and *JUL in the same SQL UDF. If you need this complexity you will need to code additional logic in the UDF.

I have found that taking the time to write these test functions pays off in the long run. For one example, it is common to copy a spool file into a physical file and then use SQL to analyze the spool file data to say, add a column of numbers or test a date. In these cases, it is handy to have functions like IsNumeric and IsDate to test data before performing a cast since spool files can often contain many formatting discrepancies.

Likewise, in ERP applications it is common to have users type in numbers or dates with special meaning into a free form character reference or comment field. There is no guarantee that a user is going to format this data properly. Again, IsNumeric and IsDate can be used to verify that the user typed something in correctly before Casting the data and risking an error.

Overall, using these validation functions can help protect your applications and queries by providing an additional bullet proofing defense. With a little practice, it only takes a few minutes to write these helper functions.


Michael Sansoterra is a programmer/analyst for i3 Business Solutions, an IT services firm based in Grand Rapids, Michigan. Send your questions or comments for Michael to Ted Holt via the IT Jungle Contact page.




                     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

Computer Measurement Group:  CMG '08 International Conference, December 7-12, Las Vegas
looksoftware:  snap the best back-end into the coolest front-end
Vision Solutions:  A $20 gas card for completing a short i5/OS DR survey


 

IT Jungle Store Top Book Picks

Easy Steps to Internet Programming for AS/400, iSeries, and System i: List Price, $49.95
Getting Started with PHP for i5/OS: List Price, $59.95
The System i RPG & RPG IV Tutorial and Lab Exercises: List Price, $59.95
The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95


 
The Four Hundred
Bytware Bought by Help/Systems and Audax

The Power Systems 570 i Edition Versus Big Windows Boxes

An Open Letter to i Shops from the Power Systems GM

System Performance Management Is Like Having Insurance

IDC and i: Next Time, Can You Talk to Some Real i Shops?

The Linux Beacon
Why Blade Servers Still Don't Cut It, and How They Might

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Microsoft Ponies Up Another $100 Million for Novell Linux

Mad Dog 21/21: Newtonian Economics

Two More Xeon-Based Galaxy Servers from Sun

Four Hundred Stuff
QJRN/400 Sniffs Out Fraud, One Journal Receiver at a Time

Databorough Beefs Up X-Analysis for Application Modernization

BCD's Presto Web Enablement Software Goes GA

IBM Promotes the i--iPhone, That Is

Valid Gets IBM Certification for i OS-Based Biometric System

Big Iron
For Some Customers, the Mainframe Is Green

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
September 20, 2008: Volume 10, Number 38

September 14, 2008: Volume 10, Number 37

September 7, 2008: Volume 10, Number 36

August 30, 2008: Volume 10, Number 35

August 23, 2008: Volume 10, Number 34

August 16, 2008: Volume 10, Number 33

The Windows Observer
Citrix Addresses Performance with XenApp 5

Server Buyers Shop Like It's 1999 in the Second Quarter

Intel Keeps Both Arms Swinging with Xeons, Jabs with Itanium

Mad Dog 21/21: Newtonian Economics

Microsoft Does Something About Those SQL Injection Attacks

The Unix Guardian
What the Heck Is the Midrange, Anyway?

Overseas and Notebook Sales Offset Printer Declines for HP in Q3

Two More Xeon-Based Galaxy Servers from Sun

Mad Dog 21/21: Newtonian Economics

Intel's Nehalems to Star at IDF, AMD Pitches Shanghai

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

THIS ISSUE SPONSORED BY:

Help/Systems
WorksRight Software
ARCAD Software


Printer Friendly Version


TABLE OF CONTENTS
Want a Fast and Easy Way To Sort Subfile Data?

Testing for Valid Data Representation in SQL

Admin Alert: When System Job Tables Attack, Part III

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Data Queues vs. MQ Series: Performance

Removing blanks from a CL Variable

XML

SQL "Hidden" Field

Java Messages

MQ Help Desired





 
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-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement