Page 1 of 1

Convert 10 bytes DATE to integer

PostPosted: Thu Oct 31, 2019 5:49 pm
by Manjucan
Hi

Suppose I have a date field declared as x(10).
I need to perform a manipulation on this date and for that purpose I need to convert it to integer.
I tried the INTEGER-OF-DATE function for this date.
But I am getting error saying the parameter for the function is invalid .
I doubt the issue is because the date I am passing as input is of the format YYYY-MM-DD but the function expects input as YYYYMMDD.
So how do I handle this ?

Re: Convert 10 bytes DATE to integer

PostPosted: Thu Oct 31, 2019 6:21 pm
by prino
One option is to use REDEFINE and MOVE CORRESPONDING, or whatever the "BY NAME" option is called in COBOL.

Re: Convert 10 bytes DATE to integer

PostPosted: Thu Oct 31, 2019 8:46 pm
by Robert Sample
There are, of course, multiple ways to handle this. Two of them are:
      WORKING-STORAGE SECTION.                                                
       01  WS-VARS.                                                            
           05  WS-SOURCE               PIC X(10) VALUE '2019-10-31'.            
           05  WS-SOURCE-R             REDEFINES WS-SOURCE                      
                                       PIC 9999B99B99.                          
           05                          REDEFINES WS-SOURCE.                    
               10  WS-SRC-1            PIC 9(04).                              
               10                      PIC X(01).                              
               10  WS-SRC-2            PIC 9(02).                              
               10                      PIC X(01).                              
               10  WS-SRC-3            PIC 9(02).                              
           05  WS-TARGET               PIC 9(08).                              
           05  WS-INT-DATE             PIC 9(07).                              
       PROCEDURE DIVISION.                                                      
           COMPUTE WS-TARGET = WS-SRC-1 * 10000 +                              
                               WS-SRC-2 * 100   +                              
                               WS-SRC-3.                                        
           COMPUTE WS-INT-DATE = FUNCTION INTEGER-OF-DATE (WS-TARGET).          
           DISPLAY WS-SOURCE ' ' WS-TARGET ' ' WS-INT-DATE.                    
           MOVE WS-SOURCE-R            TO  WS-TARGET.                          
           COMPUTE WS-INT-DATE = FUNCTION INTEGER-OF-DATE (WS-TARGET).          
           DISPLAY WS-SOURCE ' ' WS-TARGET ' ' WS-INT-DATE.              
producing results of
2019-10-31 20191031 0152975
2019-10-31 20191031 0152975
As otherwise stated, MOVE CORRESPONDING would be a third method.

Re: Convert 10 bytes DATE to integer

PostPosted: Thu Oct 31, 2019 10:34 pm
by Manjucan
Thanks for your inputs.
So you mean to say the below statement would work ??

WS-DATE PIC X(10)
WS-INT-DATE 9(8)

MOVE CORRESPONDING WS-DATE TO WS-INT-DATE

This way ws-int-date will only have numeric without the hyphen in date ?

Re: Convert 10 bytes DATE to integer

PostPosted: Fri Nov 01, 2019 12:08 am
by Robert Sample
No -- you have no understanding of MOVE CORRESPONDING. You would need to code something like this (untested):
05  WS-DATE.
    10  WS-1 PIC 9(04).
    10       PIC X(01).
    10  WS-2 PIC 9(02).
    10       PIC X(01).
    10  WS-3 PIC 9(02).
...
05  WS-INT-DATE.
    10  WS-1 PIC 9(04).
    10  WS-2 PIC 9(02).
    10  WS-3 PIC 9(02).
...
MOVE CORRESPONDING WS-DATE TO WS-INT-DATE.
MOVE CORRESPONDING works on a group-level variable (your post referenced elementary variables which cannot be used in a MOVE CORRESPONDING) and moves all same-named variables to the destination group-level variable. They do not have to be in the same order, nor do the PICTURE clauses have to match, but the names must be exactly the same. Filler won't be moved, nor will variables whose names are in the source or destination group variable only.