View previous topic :: View next topic
|
Author |
Message |
abdulrafi
Active User
Joined: 14 Sep 2009 Posts: 184 Location: Coimbatore
|
|
|
|
Hi,
Is there any function in cobol to do the following,
Input date : 1/1/2001
output date I need is: 01/01/2001
Currently my code is something like this and I am unable to achieve what I am expecting,
Code: |
01 WS-EFF-DATE1.
05 WS-EFF-MM1 PIC X(02) VALUE SPACES.
05 FILLER PIC X(01) VALUE '/'.
05 WS-EFF-DD1 PIC X(02) VALUE SPACES.
05 FILLER PIC X(01) VALUE '/'.
05 WS-EFF-YYYY1 PIC X(04) VALUE SPACES. |
Request your help on this. |
|
Back to top |
|
|
abdulrafi
Active User
Joined: 14 Sep 2009 Posts: 184 Location: Coimbatore
|
|
|
|
subject changed. |
|
Back to top |
|
|
boyti ko
New User
Joined: 03 Nov 2014 Posts: 78 Location: Malaysia
|
|
|
|
try to search for PIC Z. |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3051 Location: NYC,USA
|
|
|
|
make a use of intrinsic functions,
look for
vs
, look how redefine can be leveraged , look CEECBLDY too.
Quote: |
try to search for PIC Z. |
how would this add leading 0? |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8696 Location: Dubuque, Iowa, USA
|
|
|
|
COBOL does not have anything directly to do what you want, but the code isn't very complicated:
Code: |
77 WS-FIELD1 PIC X(10) VALUE '1/1/2001'.
77 WS-HOLD-VALUE PIC X(02).
77 WS-HOLD-FIELD PIC X(10).
77 WS-CHARS PIC 9(02).
01 WS-EFF-DATE1.
05 WS-EFF-MM1 PIC 9(02).
05 FILLER PIC X(01) VALUE '/'.
05 WS-EFF-DD1 PIC 9(02).
05 FILLER PIC X(01) VALUE '/'.
05 WS-EFF-YYYY1 PIC X(04).
...
INSPECT WS-FIELD1
TALLYING WS-CHARS FOR CHARACTERS BEFORE INITIAL '/'.
IF WS-CHARS > 0
MOVE WS-FIELD1 (1 : WS-CHARS)
TO WS-HOLD-VALUE
COMPUTE WS-EFF-MM1 = FUNCTION NUMVAL (WS-HOLD-VALUE)
MOVE WS-FIELD1 (WS-CHARS + 2 : )
TO WS-HOLD-FIELD
END-IF.
MOVE ZERO TO WS-CHARS.
INSPECT WS-HOLD-FIELD
TALLYING WS-CHARS FOR CHARACTERS BEFORE INITIAL '/'.
IF WS-CHARS > 0
MOVE WS-HOLD-FIELD (1 : WS-CHARS)
TO WS-HOLD-VALUE
COMPUTE WS-EFF-DD1 = FUNCTION NUMVAL (WS-HOLD-VALUE)
MOVE WS-HOLD-FIELD (WS-CHARS + 2 : )
TO WS-EFF-YYYY1
END-IF.
DISPLAY WS-FIELD1 ' ' WS-EFF-DATE1. |
produces output ot
Code: |
1/1/2001 01/01/2001 |
|
|
Back to top |
|
|
Bill Woodger
Moderator Emeritus
Joined: 09 Mar 2011 Posts: 7309 Location: Inside the Matrix
|
|
|
|
If your data are reliable, it is even simpler:
Code: |
01 SOURCE-FIELD PIC X(30) VALUE "1/1/2016".
01 RESULT-FIELD.
05 RF-DD PIC 99.
05 FILLER PIC X VALUE "/".
05 RF-MM PIC 99.
05 FILLER PIC X VALUE "/".
05 RF-YYYY PIC XXXX.
...
UNSTRING SOURCE-FIELD DELIMITED BY "/"
INTO RF-DD RF-MM RF-YYYY |
I normally define date-fields as PIC X. Here I have 99 for the DD and MM as I want the leading-zero-fill which is automatically associated with a MOVE to a numeric field (the UNSTRING ends up using the same rules as MOVE).
Note that the YYYY is PIC X because I don't want the properties of a numeric for a MOVE. I want left-justified and right-truncation.
If desperate for some odd reason to use 9s for the YYYY, include an additional value (SPACE) on the DELIMITED BY. |
|
Back to top |
|
|
abdulrafi
Active User
Joined: 14 Sep 2009 Posts: 184 Location: Coimbatore
|
|
|
|
Thanks Bill and Robert for your kind help. |
|
Back to top |
|
|
|