70 pts.
 Need help in moving Character field to numeric field in RPG.
Need help in moving Character field to numeric field in RPG. I have a character field of lenght 10 in the file and its was left aligned. Now I'm reading the file and get this value and moving to a numeric field of 20 length. I'm facing a problem while moving since its was a left aligned i'm not able to move to numeric field. Its populating the field with value follwed by Zeros. I need Zero prefix to the field value.

Software/Hardware used:
ASKED: December 5, 2010  1:52 PM
UPDATED: December 7, 2010  8:16 AM

Answer Wiki:
RPG or RPGLE/RPG IV??? In RPGLE it might look something like this. my20n = %dec(myChar:20:5) assumes m20n is 20, 5 but since myChar might not be a number you might put it in a monitor block. Phil
Last Wiki Answer Submitted:  December 5, 2010  5:48 pm  by  philpl1jb   44,630 pts.
All Answer Wiki Contributors:  philpl1jb   44,630 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

In RPGLE fixed format that would be
EVAL my20n = %dec(myChar:20:5)
Phil

 44,630 pts.

 

Hi Philp,

Its RPG. I’m having the data as
VAr1 – ’1234 ‘ – Char field
VAr2 – 000000000 – Numeric field

When i moved
C MOVE VAR1 VAR2 ==> VAR2 – 1234000000

Thx,
KVr

 70 pts.

 

Ohhh .. trip in the wayback machine
“When a character field is moved into a numeric result field, the digit portion of each character is converted to its corresponding numeric character and then moved to the result field. Blanks are transferred as zeros”
It sounds like you will need to rebuild the value as 0000001234
1. Move Var1 WRKVAR 10
2. Loop while position 10 of WARKVARAis a blank — this might require a substr to get value in position 10.
2.1 add a leading zero ’0′ CAT WRKVAR WRkVAR finlly WRKVAR = 0000001234
3. then MOVE WARVAR Var2
Phil

 44,630 pts.

 

Where are these values coming from? Why are you having character fields that need to be converted into numeric values?

Depending on the process, there might be better things to do.

Phil has the right answer for what you asked. I’m curious if there could have been other questions at a higher level that might change the circumstances.

Tom

 110,115 pts.

 

Hi Phil/Tom,

Thanks for the solution. I will try this and let you guys know.

Tom This value is 20 char length file field. There are some records with left aligned and i need to move to 20 numeric with leading zeros to another file as per the design.

Thanks
Kvr.

 70 pts.

 

I understand that the values are in a character field in a file. But why are they put into a file as character values in the first place? Where does the file come from? Why doesn’t it just have a numeric field? How are you getting the file? (Why would the field be 20 characters wide? That’s a big field for numeric values.)

A description of the general process would be helpful. For some kinds of files, it might be possible to do it without programming. For example, you might create a VIEW over the file so that the character column is converted to a numeric value with the DECIMAL() SQL function.

Tom

 110,115 pts.