Cobol mainframe - check numeric on alpha or numeric field?

   

  Asked by Anonymous - Cobol mainframe - check numeric on alpha or numeric field?:
I have a numeric field defined as
05 WS-NUM-IN           PIC 9(05).

The question I have is sometimes garbage gets populated in this field, because a PIC X(05) char field is getting moved to this elsewhere, so for example I want to check when its zero or garbage without abending or causing any type of S0C7 error.  

I should be doing a "is numeric" check to avoid any type of S0C7 but would that be checked on the char field X(05) or this 9(05) field above?  I'm surprised it didnt abend moving garbage into this field already.

Anonymous Says:
It will abend with a s0c7 whenever you try to access that field, but not when you move data to it. You should always validate a scenario like this just because it will prevent future abend someday down the road as you never know when the input data will change especially if its coming from elsewhere.

I would code the check like this.

Set flag-good to true
If WS-NUM-IN is numeric
   If WS-NUM-IN = 0
       Set flag-good to false
   End-if
Else
   Set flag-good to false
End-if

That will cover it either way if there any type of garbage in the field or zeros in the field.

Add your reply below ...


Your Nickname:

Your Reply:

Type the code: