You are on page 1of 3

# How to MOVE an Alphanumeric Field To Numeric field.

An Alphanumeric to numeric move is not a valid move in COBOL. There is an exceptional case where this move is permissible which will be discussed in the text. The Moves in Cobol are always guided by the 'Receiving field' in Cobol. If the receiving field is declared as numeric then the move is called a numeric move. Let us take an example and analyze each case along with their solutions. Let us consider the following fields: WS-INPUT WS-OUTPUT PIC X(10). PIC 9(10).

The following Table shows the Input records and the Expected Output Case 1. 2. 3. INPUT RECORD 1234567890 10107 101006 OUTPUT RECORD 1234567890 0000010107 0000101006

CASE 1: (Exceptional case) COBOL statements: MOVE WS-INPUT TO WS-OUTPUT. DISPLAY “ OUTPUT IS : “ WS-OUTPUT. Result: OUTPUT IS : 1234567890 Here even tough the Input field is Alphanumeric but as the Data in the field is numeric and has no spaces. Thus the simple MOVE statement will work.

CASE 2 & 3: COBOL statements: MOVE WS-INPUT TO WS-OUTPUT. DISPLAY “ OUTPUT IS : “ WS-OUTPUT.

ERROR!
The reason being there are spaces in the input data, which are Characters. And we cannot move characters into a numeric field. The simple MOVE statement will not work. Now the Question arises what to do if we need the numeric values of an alphanumeric variable? The above result can be achieved in 3 ways: 1. Using an array. 2. Using INSPECT statement. 3. NUMVAL () Function.

1. Using an array:
01 WS-ARR. 05 WS-INPUT-ARR 01 WS-IDX OCCURS 10 TIMES PIC X (1). PIC 9(02) VALUE ZEROES.

MOVE WS-INPUT TO WS-ARR. PERFORM VARYING WS-IDX FROM 1 BY 1 UNTIL WS-INOUT-ARR (WS-IDX) IS NOT NUMERIC. MOVE WS-INPUT (1:WS-IDX) TO WS-OUTPUT. DISPLAY “ OUTPUT IS : “ WS-OUTPUT. Result for INPUT RECORD 2: OUTPUT IS : 0000010107 Result for INPUT RECORD 3: OUTPUT IS : 0000101006 This particular method is not considered a good programming practice.

2. Using INSPECT Statement:
* ************************************************************** IT COUNTS THE NUMBER CHARACTERS BEFORE THE FIRST SPACE * * IS ENCOUNTERED AND MOVES THE VALUE OF THAT COUNT TO WS-IDX * **************************************************************

INSPECT WS-INPUT TALLYING WS-IDX FOR CHARACTERS BEFORE INITIAL SPACES. MOVE WS-INPUT (1:WS-IDX) TO WS-OUTPUT. DISPLAY “ OUTPUT IS : “ WS-OUTPUT. Result for INPUT RECORD 2: OUTPUT IS : 0000010107 Result for INPUT RECORD 3: OUTPUT IS : 0000101006

WAIT THERE IS A CATCH HERE!!
The INSPECT may FAIL if WS-INPUT is “ 123 “. Here the value of the WS-IDX = 0. Thus you will get an error, as you will be moving spaces. (By now we know that spaces cannot be moved in a numeric variable) The above case is handled by the next function.

3. Using NUMVAL () Function:
The NUMVAL function returns the numeric value represented by the character string specified by parameter-1. Leading and trailing spaces are ignored. Syntax FUNCTION NUMVAL (parameter-1) The total number of digits in parameter-1 must not exceed 18. COMPUTE WS-OUTPUT = FUNCTION NUMVAL (WS-INPUT). DISPLAY “ OUTPUT IS : “ WS-OUTPUT. Result for INPUT RECORD 2: OUTPUT IS : 0000010107 Result for INPUT RECORD 3: OUTPUT IS : 0000101006 Considering the case which failed in the INSPECT statement If WS-INPUT is “ 123 “ OUTPUT IS : 0000000123