Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Char to Packed Decimal 1

Status
Not open for further replies.

gdkz

Programmer
Nov 21, 2002
44
0
0
US
I have no clue with rpg and I am trying to convert some data. So please be kind.
I am trying to convert a character value into a packed decimal using rpg ile.
Here is what I was thinking, pass in a value like ('0555.000') into the procedure. Pack the data and return the packed decimal representation. The code I am using :
Code:
HNOMAIN                                        
DPD73             PR             7p 3          
DCHARIN                          8    CONST    
PPD73             B                   EXPORT   
D                 PI             7p 3          
DCHARIN                          8    CONST    
DCHAROUT          DS             4             
DDECOUT                          7P 3          
C                   EVAL      CHAROUT = CHARIN 
C                   RETURN    DECOUT           
P                 E
I may be way off base here, just looking for some direction. I will be calling compiling this as a service program and using a sql rpg ile function to call this.

Thanks,
Greg
 
Hi gdkz,

You do not need any procedure to do this job, sql can convert (cast) the fields and do the job for you.

e.g., if definition of MyAlphaFld is 7A, type simply :

Select cast(MyAlphaFld as Dec(7, 3)) as MyPackedFld, ...
or
Update ... Set MyPackedFld = cast(MyAlphaFld as Dec(5, 0)) ...

The Cast function will convert (cast) the alphameric field to packed numeric.

See page 78 for further reference on the cast function.

However if you goal is to write your own function, please let me know and I'll help you as best as I can.

hth
Philippe --
 
You can do it with one calc line

EVAL DecimalField = %dec(CharField:3)


(The "3" is number of decimals in result field.)
 
What you say is correct TalkTurkey and works fine as I have described, but maybe I should have asked the question better.
I have a DB2 physical file with a field that is defined as character, a cobol program writes a value to part of this field as a packed decimal value, we have taken an RPGIV function to read this data from a SQL select function call. Now I wish to write the value back to this field in packed form. So I need to take a decimal value, convert/pack it and write it to the character field as the packed representation of the value, which will be used by a cobol program. Confusing huh?
arrow483 will the calc line you suggest work in RPGIV or do I need free form RPG. I know little to none about RPG, so I thank both of you for your help and patience as I learn on the fly.
Thanks

Thanks,
Greg
 
If I get it well, you want to reverse the initial process and save the packed format in a char field without conversion.
E.G. the value 555.000 in packed representation 7p 3 is X'0555000F'. You want this packed value to remain unchanged and saved in packed format in the resulting alpha field.
So, could the following sql stm where I use HEX & DECIMAL functions do the job for you?

Update ... Set MyAlphaFld = hex(decimal(MyPackedFld, 7, 3)) ...

(Type indifferently decimal or dec)

Pls give it a try.

Philippe --
 
Thanks that works for me in one way great. The value I select from the character field in the physical file is a decimal 7p 3 representation stored in 4 characters. So when I need to write back to that file, I need to put it in the same 4 characters. The full hex value is too large for this. Is there a way to get it to the 4 character packed value of the hex representation.
Thanks, you have been a great help.

Thanks,
Greg
 
No, sorry, I do not see any sql BIFs that could do this so special job,
so each one has to write its own function.
I copied the procedure from your first post and adapted the function
I wrote for you. I called it "CHAROUT" but unfortunately it is
limited to input solely a 7p 3 field and output exclusively a 4 char
field, hence really focused to this unique need.
That might help anyway :eek:)

Code:
1/ MYLIB/QSRVSRC.CHAROUT

  /* ================================================== */ 
  /* New exports MUST be added at the end.              */ 
  /* Old exports MUST NEVER be removed from the list.   */ 
  /* !!! The order MUST NEVER CHANGE !!!                */ 
  /* ================================================== */ 
            STRPGMEXP                                      
            EXPORT     SYMBOL('CHAROUT')                   
            ENDPGMEXP                                      
--------------------------------------------------------------------

2/ RPGLE Function

      *-------------------------------------------------------------
      * Usage:                                                              
      * 1/ CRTRPGMOD MODULE(MYLIB/CHAROUT)             +                          
      *              SRCFILE(MYLIBSRC/QRPGLESRC)       +                          
      *    TEXT('Packed fld to Char fld w/o conversion')   
      *                                                                       
      * 2/ CRTSRVPGM SRVPGM(MYLIB/CHAROUT)             +               
      *    MODULE(*SRVPGM)                             +     
      *    EXPORT(*SRCFILE)                            +     
      *    SRCFILE(MYLIBSRC/QSRVSRC)                   +     
      *    SRCMBR(*SRVPGM)                             +     
      *    TEXT('Packed fld to Char fld w/o conversion')                                 
      *-------------------------------------------------------------
      * Function name: CHAROUT. 
      *
      * This function returns input packed field to output char field 
      * without conversion.                                                  
      *-------------------------------------------------------------

     H NoMain                                                               
                                                                            
     D CharOut         PR             4                                     
     D Pd73                           7p 3 CONST                                 
                                                                            
     P CharOut         B                   EXPORT                           
     D CharOut         PI             4                                     
     D Pd73                           7p 3 CONST                  
                                                             
     D CharDS          DS             4                      
     D Dec73                          7p 3 Overlay( CharDS ) 
                                                             
     C                   Eval      Dec73 = Pd73              
     C                   Return    CharDS                    
                                                             
     P CharOut         E      

----------------------------------------------------------------------
3/ SQL Create Function

CREATE FUNCTION MYLIB/CHAROUT (DEC (7,3))    
  RETURNS CHAR(4)                              
  LANGUAGE RPGLE                               
  DETERMINISTIC                                
  NO SQL                                       
  RETURNS NULL ON NULL INPUT                   
  NO EXTERNAL ACTION ALLOW PARALLEL            
  SIMPLE CALL                                  
  EXTERNAL NAME 'MYLIB/CHAROUT(CHAROUT)'

BTW, I seem that your function cannot work if DECOUT does not overlay CHAROUT.
 
Talkturkey that worked exactly the way I needed. Thanks for your patience on my learning curve to the Cobol/RPG/DB2 World.
So far has been quite the learning experience.
Thanks!!


Thanks,
Greg
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top