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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting MOVE to /free 1

Status
Not open for further replies.

iSeriesCodePoet

Programmer
Jan 11, 2001
1,373
US
The big thing with /free is getting rid of the MOVE statements. If you want help, post the MOVE code you have with the field definitions, then someone else or yourself can post the /free equivalent. We will try and use this for complete MOVE to /free documentation. I will also create a FAQ pointing to this thread.

Make sure to use
Code:
[ code ] [ /code ]

Me first:

Text to Number:
Code:
D Number  S  5  0
D Text    S  5
C              MOVE    Text    Number

Convert date:
Code:
D Date    S  8  0
D TempDate     D
C   *ISO       MOVE    Date     TempDate  // YYYYMMDD
C   *USA       MOVE    TempDate Date      // MMDDYYYY

How do you convert these?

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
Text to Number:

Define a data structure with a single subfield defined as a zoned decimal. Consider the following:
Code:
D TextNbr                3    Inz('300')
D CharNbr         Ds
D  Number                3S 0
.
.
.
/Free
.
.

  CharNbr = TextNbr
.
.
.
/End-Free
Number now has a numeric value of 300.
Limitations: You cannot use the LIKE keyword when defining the numeric subfield. The subfield MUST be defined as a zoned decimal. When converting a character dollar amount to a zoned decimal dollar amount, make sure you remove all non-numeric characters. (Check out my replies in the "ORIF STATEMENT" post.

Convert Date:

This is very easy in RPG IV. You just need to take advantage of the built in date functions.
Code:
D DateUSA            D DatFmt(*USA)
D DateISO            D DatFmt(*ISO)
.
.
/Free

DateUSA = %Date();
DateISO = DateUSA;

/End-Free

It's that easy. now, to convert from the Date data type to a numeric or character gets a little tricky.

Code:
D CharDate      S       8     Inz(*Blanks)
D
D CnvrtDate     Ds
D  NumDate              6 S 0
  
D DateMDY       S         D   DatFmt(*MDY)
.
.
.
/Free

 DateMDY = %Date();
 CharDate = %Char(DateMDY:*MDY);
 // CharDate now equal "09/12/03"
 CnvrtDate = %Char(DateMDY:*MDY0);
 // NumDate now equals 091203.

/End-Free

Using *MDY adds the slashs to the character output field. Using *MDY0 removes the slashs, thus letting me convert the date field from date data type to numeric data type.


RedMage1967
IBM Certifed - RPG IV Progammer
 
Hmm... interesting. I didn't you that eval did that conversion. I will have to try that out.

Let the stars start rolling!

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
"I didn't you that eval did that conversion"

What were you trying to say?

RedMage1967
IBM Certifed - RPG IV Progammer
 
No problem. Look into other date operations, like %Days, %Months, %Years. Lets you add to a date, without having to figure out if you changed months, years or are in a leap year. They system takes care of that for you.
Lets say the due date is ten days from the current date. Your code would look like this:

Code:
D CurrDate              D DatFmt(*MDY) 
D DueDate               D DatFmt(*MDY)
.
.
.
/Free

 CurrDate = %Date();
 DueDate = CurrDate + %Days(10);

/End-Free

Its that easy.
On my previous post, I might have some things in the wrong spot on my "D" specs, if I do, I aplogize.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Okay, I have time now. (Not long eh?)

This is my thing. I will have the user enter in a date in numeric in MMDDYYYY format. My program then has to convert it to numeric YYYYMMDD format. How do I do this in free format?

D FromDate S 8 0

D Temp Date D

I know in move it is
C *USA Move FromDate TempDate
C *ISO Move TempDate FromDate

How do I do this? This is the most common change I do.

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
I think this will work.

Code:
D InDate          S              8S 0 
D FromISO         S               D   DatFmt(*ISO)
D FromUSA         S               D   DatFmt(*USA)
D                    
D CharDate        DS 
D  FromDate                      8S 0
 //----------------------------------------------------
 /Free               
                          
 // convert the input date (InDate) to a USA date format
 FromUSA = %Date(InDate:*USA);
 // convert the USA date to a ISO Date
 FromISO = FromUSA;
 // this step will convert the ISO date back into a 
 // numeric value in YYYMMDD format.
 // the *ISO0 converts the ISO date into a char without
 // the slashes, which then can be cleanly converted into
 // a numeric value 
 CharDate = %Char(FromISO:*ISO0);
                        
 /End-Free
 //---------------------------------------------------


RedMage1967
IBM Certifed - RPG IV Progammer
 
That's what I used to do, until I found out about the data structure trick.

RedMage1967
IBM Certifed - RPG IV Progammer
 
True, with the MOVE cmd, it's less code, I just don't like to code fixed "C" specs anymore. Kinda gotten into the habit of thinking in terms of what works in free-form. About the only thing I can't do in free form (V5R1) is the KLIST and KFLD. There's a work around for that in V5R2, but, I'm not there yet.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Okay, here is another one.

Code:
C                   Move      DSDate        DSDateTxt

DSDate is defined as a date format (assumed *ISO)
DSDateTxt is a 10 char field. I do want the '-' in the field.

How would this be done?

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
Sorry, been busy at work.

That should work, so long as your charvar is defined long enought (12 characters, I think). The compiler will let you know.

RedMage1967
IBM Certifed - RPG IV Progammer
 
Okay, I get a compiler error on this? Can you help?

Code:
  CmpnyNum = %dec(%subst(JobName:3:3));

What is wrong with this?

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
Finish the %Dec bif. You might need all three paramters:

This is what IBM says:
%DEC(numeric or character expression{:precision:decimal places})

%DEC converts the value of the numeric or character expression to decimal (packed) format with precision digits and decimal places decimal positions. The precision and decimal places must be numeric literals, named constants that represent numeric literals, or built-in functions with a numeric value known at compile-time.

Parameters precision and decimal places may be omitted if the type of expression is neither float nor character. If these parameters are omitted, the precision and decimal places are taken from the attributes of numeric expression.

If the parameter is a character expression, the following rules apply:

The sign is optional. It can be '+' or '-'. It can precede or follow the numeric data.
The decimal point is optional. It can be either a period or a comma.
Blanks are allowed anywhere in the data. For example, ' + 3 ' is a valid parameter.
The second and third parameters are required.
Floating point data, for eample '1.2E6', is not allowed.
If invalid numeric data is found, an exception occurs with status code 105.


Try this:
Code:
 CmpnyNum = %Dec(%Subst(JobName:3:3):3:0);

RedMage1967
IBM Certifed - RPG IV Progammer
 
Here are some date conversions you can do in 1 line of code.
I know these work at v5r2 but may work on earlier releases.

// Convert Date from ccyymmdd to mmddyy
Sdt = %uns(%char(%Date(#SoSdt:*Iso):*Mdy0));

// Todays Date in Ccyymmdd format
Today = %uns(%char(%Date():*Iso0));

// Convert From Julian to ccyymmdd
CbpPayDte = %Dec(%Char(%Date(%Subst(%Editc(Rpdgj:'X'):2:5)
:*Jul0):*Iso0):8:0);

T. Bishop
 
as400pro Those are a bit complicated to remember.

redmage Thanks, I will try that. The documentation on that biff was confusing.

iSeriesCodePoet
iSeries Programmer/Lawson Software Administrator
[pc2]
See my progress to converting to linux.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top