fhg
Volume 6, Number 38 -- October 18, 2006

The SPACE Function Takes Up a Lot of Space

Published: October 18, 2006

Hey, Ted:

I have to build a file for input to another system. All fields are fixed-length, and numeric values are to be edited and right-adjusted. SQL's CHAR function takes care of the editing requirement, but the result is left-adjusted. How do I right-adjust an edited numeric value? Or will I have to rewrite my program using native I/O operations?

--Sarah


On the surface, this seemed like an easy request. It turned to be a little more complicated than I had thought.

I suggested Sarah use the SPACE function to generate enough leading blanks to force right alignment. For example, the following expression right-adjusts an edited version of field ABUBAL within an area of twenty bytes.

SELECT space(20 - length(trim(char(ABUBAL))))||trim(char(ABUBAL)), ...

The SPACE function takes one argument--the number of spaces to be generated. Suppose that the edited ABUBAL occupies seven bytes. SPACE returns 13 blanks, which are concatenated to the edited ABUBAL.

IMHO, this expression deserves its own spot in the county landfill. However, it works. Well, it works up to a point. Here was Sarah's final query.

INSERT INTO ABMASTF
SELECT ABTRNS, ABCUS, ABTDT,
  space(20 - length(trim(char(ABUBAL))))||trim(char(ABUBAL)),
  digits(ABNDD),
  space(20 - length(trim(char(ABTAMT))))||trim(char(ABTAMT)),
  ABREF, ABPONO, ABDIV,
  space(20 - length(trim(char(ABTAX))))||trim(char(ABTAX)),
  space(20 - length(trim(char(ABFRGT))))||trim(char(ABFRGT)),
  space(20 - length(trim(char(ABOTH))))||trim(char(ABOTH)),
  ABTTYP,
  ABCHKN, ABPNUM, ABRCDE, ABSLSN, ABSHPT, ABSRCI, ABTCCD,
  space(20 - length(trim(char(ABTCAM))))||trim(char(ABTCAM)),
  space(20 - length(trim(char(ABTCBA))))||trim(char(ABTCBA)),
  space(20 - length(trim(char(ABLCAM))))||trim(char(ABLCAM)),
  space(20 - length(trim(char(ABLCBA))))||trim(char(ABLCBA)),
  ABDIVC
FROM ABmast

At this point, her SQL command cancelled with error message SQL0101.

SQL statement too long or complex. ... 3 - The sum of the lengths of the non-LOB columns in a select list, table, view definition, or user defined table function is greater than 32766 or the definition contains a LOB and the sum of the lengths specified on the ALLOCATE clause for varying-length fields and the non-varying field lengths is greater than 32740. The maximum length is reduced if any of the columns are varying-length or allow null values.

I knew the field lengths did not add up to 32,766 bytes, so I contacted IBM for help with this one. Thanks to Sue Ramono and Jeff Tenner, I learned that the SPACE function returns a 4000-byte result. Sue and Jeff suggested using the SUBSTR (substring) function instead.

substr('                    ',1,
        20-length(trim(char(ABUBAL)))||trim(char(ABUBAL)),

The first argument is twenty spaces surrounded by single quotes. The second argument tells the position of the first blank to return (i.e., position one), and the third argument is the number of blanks to return. It's still ugly, but SUBSTR uses less resources. (In all fairness, I should point out that SQL is designed to retrieve data, not format it.)

While I was waiting to hear back from IBM, I toyed with a user-defined function to right-adjust a value. Here is the RADJ function, hastily thrown together and unproven in production.

create function mylib/radj                           
   (inString varchar(256), inLength integer)           
   returns varchar(256)                                
   language SQL                                        
   contains SQL                                        
   deterministic                                       
   returns null on null input                          
   no external action                                  
   begin                                               
      if inLength < length(trim(inString))             
         then signal sqlstate '22003'                  
              set message_text = 'Length is invalid.'; 
      end if;                                          
      return                                           
        (space(inLength-length(trim(inString))) concat 
         trim(inString));
   end

RADJ takes two arguments--the character value to be right-adjusted and the size of the area in which it is to be right-adjusted. I was able to run queries with many RADJ functions without getting the SQL0101 error.

SELECT radj(char(ABUBAL),20) ...

What an interesting profession! I learn something new every day.

--Ted



Sponsored By
PRODATA COMPUTER SERVICES

Demand DBU
and get it for $10 a day!

When you need to get to your data NOW, use DBU-on-Demand
and activate for one day or for any number of days.
This is great for those "seldom touched" systems without a database utility
and a lifesaver in emergency database situations.
Use DBU for $10 a day anytime, anywhere!

Do it with DBU.

It's Server Proven and on IBM’s Roadmap!
Call us today to access DBU-on-Demand.
800.228.6318

www.DoDBU.com



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

Sponsored Links

Canvas Systems:  We build and deliver custom iSeries rental solutions
Integrated Print Solutions:  Print AFP/IPDS documents to any network printer
COMMON:  Join us at the Spring 2007 conference, April 29 – May 3, in Anaheim, California

 


 
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