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!

Extracting Parts of Several Numeric Fields to create one 2

Status
Not open for further replies.

rstitzel

MIS
Apr 24, 2002
286
US
We use an odd date format. Year Week Day YYWWD.

I have four variables:

CurYear 4 0
WeekOfYear 5 0
DayNum 1 0
YYWWD 5 0

Example Values:

CurYear = 2004
WeekOfYear = 00019
DayNum = 2

I want YYWWD to equal 04192. I can do this using a couple of extra variables, several lines of code and by using MOVE and MOVEL. I'm sure there's a 'better' way of doing this or at least a more modern way.

If anyone can post some code that doesn't use move or movel to accomplish what I want, it would be very much appreciated.

Thanks in advance for any and all help.
 
rstitzel,

How about:

Code:
YYWWD = %CHAR( CurYear ) + %CHAR( WeekOfYear ) + %CHAR( DayNum );

HTH,
MdnghtPgmr
 
Quite right MdnghtPgmr !
However I believe that
Code:
yymmd = %int(%subst(%char(CurYear):3:2) 
                  + %char(WeekofYear) 
                  + %char(DayNum));
the substring "%subst(%char(CurYear):3:2)" should closer fit to the rstitzel's needs coz' wants solely the last 2 digits in the year.
 
Talkturkey,

You are quite right! I missed that. Just like I initially missed the %INT.

Thanks for the correction!

MdnghtPgmr
 
Talkturkey,

No, not really. Why? Oh, I get it, MdnghtPgmr! Okay, the handle comes from my eariler days in I/T. LOL! I should rename myself 8to5Pgmr!

Still laughing...
MdnghtPgmr
 
I'm on 5.1 and can't use %INT with a character field ONLY numeric! That stinks. We have a specific reason for not going to 5.2 right now (unsupported hardware) so I'm using some code I found on this site that does the conversion. A whole program to do what a BIF can do...oyyyy.

Thanks again for your help. Good examples for future reference on how to use the %int, %char and %subst BIF's.

RS
 
If %INT doesn't work because you're still on V5R1, look here.



De mortuis nihil nisi bonum.

 
Yes thank you I found that link from your eariler post. Very Helpful.
 
Hi rstitzel,
I think of a new solution that could do the job:
Code:
d  MyDsIn         ds            10                       
d  CurYear                       4S 0 inz(2004)          
d   Year2LDA                     2a   overlay(MyDsIn:3)  
                                                         
d  WeekOfYear                    5S 0 inz(00019)         
d   WeekOfYearA                  5a   overlay(MyDsIn:5)  
                                                         
d  DayNum                        1S 0 inz(2)             
d   DayNumA                      1a   overlay(MyDsIn:10) 
                                                         
d  MyDsOut        ds             5                       
d  yymmd                         5S 0                    
d  yymmdA                        5a   overlay(MyDsOut:1) 
...
c                   Eval      yymmdA = Year2LD+%subst(WeekOfYearA:4:2)+
c                                                           DayNumA   
c                   dsply                   yymmd
There is also an other solution with sql:
Code:
c/Exec sql                                            
c+ Set :yymmdA =                                      
c+  Substr(Cast(:CurYear    as Char(4)), 3, 2)  concat
c+         Cast(:WeekOfYear as Char(2))         concat
c+         Cast(:DayNum     as Char(1))               
c/End-exec  
c                   dsply                   yymmd

Notice in the first solution that I have redefined the numeric fields in alphanumeric and use them only in the Eval stm while in the 2nd solution I use directly the numeric fields that I convert (cast) to alphameric fields with Cast function and concatenate together on the sql Set stm.
Notice also that in both cases I display the yymmd field and not the redefined yymmdA field to show I get the desired results in yymmd field.

Which one do you prefer ? The 1st one avoids sql but implies redefinition of the fields, the 2nd involves sql but no redefinition.

hth
Philippe --
 
Tall Turkey:

I'll give your SQL code a shot. I was actually having a problem with the original code. When the WeekOfYear has a leading zero when coverted to alpha it drops the leading zero and I NEED the leading zero to be there. For example January 5, 2004 should be 04021 what I'm getting is 04210.

I'll let you know how it goes. Thank you!
 
I should have definitely said that the SQL code goes wrong coz of WeekOfYear while the RPG code goes right...
Tall Turkey
 
rstitzel,

When converting from digits to character in RPG and you want to keep the leading zero use EDITC( VarName : 'X' ).

HTH,
MdnghtPgmr
 
rstitzel,

Man, I just can't do anything right in this thread! ;-) That should have been:

free format
Code:
CharVar = %EDITC( NumVar : 'X' );

or

fixed format
Code:
Eval CharVar = %EDITC( NumVar : 'X' )

HTH,
MdnghtPgmr
 
MdnghtPgmr,
Man, this one keeps the leading zero rollin'.
(YYWWDA overlays YYWWD). Give it a try ! :eek:)
Code:
/free
     YYWWDA = Year2LD+%subst(WeekOfYearA:4:2)+ DayNumA; 
     dsply  YYWWDA;   
/end-free
 
Thank you. I got it working. Thanks to MdntPrgmr & Tall Turkey for your posts!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top