Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
Date Comparison in COBOL

Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message

New User

Joined: 12 Apr 2007
Posts: 21
Location: Coimbatore

PostPosted: Mon Dec 24, 2007 10:28 pm    Post subject: Date Comparison in COBOL
Reply with quote

I have 2 date fields Date1 and Date2. They are both in MYY format i.e., a single byte month and 2 byte year. The month values are '1' '2' '3' '4' '5' '6' '7' '8' '9' '0' '-' '&' i.e., Month is alphanumeric and Oct, Nov and Dec are assigned with '0' '-' and '&' respectively.

I have to execute a paragraph if date1>date2.

The only logic that comes to my mind is regenerating 2 byte month from single byte month and assign century to year field. But this requires lot of variables in addition to the input file variables. Also lot of IF is used.

Can you help me in optimising the comparison?
Back to top
View user's profile Send private message


Active Member

Joined: 13 Jun 2007
Posts: 632
Location: Wisconsin

PostPosted: Tue Dec 25, 2007 8:18 am    Post subject:
Reply with quote

Whoever designed your input used poor data design.
Using one character for a month indicator is horrible when you have a known set of 12 and they gave a later month a sequence number lower than an earlier month. Only 2 bytes for a year because you can't tell when a century changes from say 2000 to 2100. If you aren't given the century then the best you are doing is guessing. If you have been guarenteed (not your own assumption) that the input years will always fall in the same century.


01 ws-input-date1
  05 ws-date1-m pic x.
    88 Dec1 value '&'.
    88 Nov1 value '-'.
    88 the rest of the months
  05 ws-date1-yy pic 9(2).
01 ws-input-date2
  05 ws-date2-m
    88 Dec2 value '&'.
    88 Nov2 value '-'.
    88 the rest of the months
  05 ws-date2-yy pic 9(2).

if (ws-date1-yy > ws-date2-yy)
   perform needed-paragraph
   if (ws-date1-yy = ws-date2-yy) AND (ws-date1-m NOT = ws-date2-m)
      if (Dec2 or Nov2 or Oct2) OR (Dec1 or Nov1 or Oct1)
         if dec1
            perform needed-paragraph
*here is where the messy part is, but I am too tired to write it for you now
*if you are any good, you should be able to come up with some date logic
* here yourself. it is not very easy because of poor data design and you
*don't want to reformat the date to something resonable that you can do
*numeric operations on. But then again it shouldn't be terribly difficult.
         if (ws-date1-m > ws-date2-m)
            perform needed-paragraph
end if
Back to top
View user's profile Send private message

Active User

Joined: 05 Dec 2006
Posts: 153
Location: Seattle, WA

PostPosted: Wed Dec 26, 2007 9:49 pm    Post subject: Reply to: Date Comparison in COBOL
Reply with quote

I work on a very archaic system where the dates are stored in MMDDYY on all flat and VSAM files, but have full DB2 dates on the tables.

So every program we have that uses any kind of date comparison has to use some sort of date conversion.

We have chosen to use date windowing as a stop-gap measure while the system is slowly converted (one file at a time) to using the CCYY-MM-DD format.

For date comparisons, we convert the dates to CCYYMMDD format. This involves code like this:


      03  WS-COMP-1-CC                 PIC 9(02).
      03  WS-COMP-1-YY                 PIC 9(02).
      03  WS-COMP-1-MM                 PIC 9(02).
      03  WS-COMP-2-DD                 PIC 9(02).

      03  WS-COMP-2-CC                 PIC 9(02).
      03  WS-COMP-2-YY                 PIC 9(02).
      03  WS-COMP-2-MM                 PIC 9(02).
      03  WS-COMP-2-DD                 PIC 9(02).

(in the copybook, the DB2 date is broken down by each portion of the date)



(do a similar move for the other date to WS-COMPARE-DATE-2 fields)

    do something
    do something else

Pathetic, but it works. Thank God we are a government agency and can get away with lousy performance.
Back to top
View user's profile Send private message

Active User

Joined: 24 May 2006
Posts: 258
Location: Salem, Oregon

PostPosted: Wed Dec 26, 2007 11:56 pm    Post subject:
Reply with quote

You can also use the date functions.

01 ws-date-area.
05 ws-compare-date-1 pic 9(08) value zeroes.
05 ws-compare-date-2 pic 9(08) value zeroes.

ws-input-date-1 and ws-input-date-2 are your YYMMDD input dates.
they account for the correct century or you can adjust the DATE WINDOW
to allow for years less than 50 years prior or 50 years greater then the
current year.

compute ws-compare-date-1 = FUNCTION DATE-TO-YYYYMMDD
compute ws-compare-date-2 = FUNCTION DATE-TO-YYYYMMDD

IF ws-compare-date-2 > ws-compare-date-1
Back to top
View user's profile Send private message
Warnings : 1

New User

Joined: 15 May 2006
Posts: 92

PostPosted: Fri Jan 11, 2008 4:45 pm    Post subject:
Reply with quote

If the date is in DDMMYY format .... then can it be converted to DDMMYYYY format ??

my question is like ... if I get the date 011020 ... would it be converted to 01101920 OR 01102020 ????

Any kind of help in this regard would be really appreciated.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1


Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Linkage editor is part of COBOL, z/OS... ankit.jain COBOL Programming 4 Fri Apr 20, 2018 4:17 pm
This topic is locked: you cannot edit posts or make replies. Difference between EQUAL and EQUAL TO... jithinraghavan COBOL Programming 3 Thu Apr 19, 2018 2:22 am
No new posts Call COBOL or PLI with a click from w... vaibhav gs All Other Mainframe Topics 1 Mon Apr 16, 2018 7:29 pm
No new posts COBOL MVS options BiswajitDG COBOL Programming 8 Fri Apr 06, 2018 12:35 am
No new posts batch cobol/db2 paragraph fall thru a... alois eisshofer COBOL Programming 5 Wed Mar 14, 2018 12:21 am

Back to Top
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us