Datatype Conversion Scenario interview question in Cobol

Chanikya AChanikya A
2 min read

Assume a Scenario:

You have an alphanumeric field X(10) that holds numeric data (but as characters), and you need to convert it to a numeric field 9(10).

In COBOL, the data types X(10) and 9(10) represent two different kinds of fields:
X(10): A string of 10 characters (alphanumeric data).
9(10): A numeric field with 10 digits.

Example:
X(10) (alphanumeric): '1234567890'
9(10) (numeric): 1234567890

To convert a data field from X(10) to 9(10), you are typically converting an alphanumeric string (which may contain numbers, letters, or special characters) into a purely numeric value. This conversion process will only work correctly if the alphanumeric data contains valid numeric characters (0-9), otherwise, it may result in an error or unexpected behavior.

*Steps to Convert in COBOL

->Define the Data Structures

DATA DIVISION. WORKING-STORAGE SECTION.

01 ALPHANUMERIC-FIELD PIC X(10). This is the X(10) field (alphanumeric)
01 NUMERIC-FIELD PIC 9(10). This is the 9(10) field (numeric)Converting the Alphanumeric Value to Numeric:

PROCEDURE DIVISION.
MOVE '1234567890' TO ALPHANUMERIC-FIELD Assign alphanumeric value*—>Convert ALPHANUMERIC-FIELD to NUMERIC-FIELD
MOVE FUNCTION NUMVAL(ALPHANUMERIC-FIELD) TO NUMERIC-FIELD

DISPLAY 'Alphanumeric: ' ALPHANUMERIC-FIELD DISPLAY 'Numeric: ' NUMERIC-FIELD
STOP RUN.

* Function NUMVAL:

-> The NUMVAL function in COBOL is used to convert an alphanumeric string that contains only numeric characters into a numeric data type. If the string contains non-numeric characters (like letters or symbols), it will cause an error or produce unexpected results, so the data should be validated before attempting the conversion.

-> In this example, '1234567890' is a valid numeric string, so it is converted to the numeric field NUMERIC-FIELD successfully.

-> To convert an X(10) (alphanumeric) to a 9(10) (numeric), use the NUMVAL function in COBOL.Ensure that the alphanumeric field contains only numeric characters before conversion. You may need to validate or clean the data to avoid errors.

0
Subscribe to my newsletter

Read articles from Chanikya A directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Chanikya A
Chanikya A

8+ years of progressive experience in Mainframe Development, Migration, Enhancement, Maintenance and Support projects. • Extensive Knowledge in requirement gathering, analysis, design, development, implementation, testing, integration deployment, production support, documentation and maintenance of IBM Mainframe applications. • Extensively Design, develop and maintain complex software systems in Mainframe ensuring high quality and timely delivery. • Extensively worked on COBOL, JCL, DB2, VSAM, and CICS. • Extensively worked on Changeman, Endevor, File-aid, Git, DB2 File Manager, Connect Direct, Zeke, Jira, CA7, and Servicenow Tools • Familiarity with mainframe tools such as REXX, TSO, ISPF, and SDSF. • Having Experience in Banking and Healthcare domain. • Having Experience in Linux Shell Scripting. • Having Good verbal, written and interpersonal skills. • Having experience in develop documentation throughout the software development life cycle (SDLC) • Strong experience on co-ordination skills to work with offshore/onshore teams throughout all phases. Operating System : Linux, IBM Mainframe Z/OS, IBM Mainframe MVS, Windows 7 & 10 Programming Languages: COBOL, JCL Database & Access: DB2, CICS, VSAM, SQL Tools and Utilities: IBM IDz,TSO, ISPF,GIT,SDSF, REXX, File-aid, File Manager , Sonarqube, Jira , Servicenow, Rally, Endevor, Changeman, CA7, Zeke, Savrs, and Fault Analyzer Domain : Banking and Healthcare Debugging Tool : Xpeditor Version Control Tools : Endevor , Changeman, and Git Reporting Tools : MS Excel Presentation Tools : MS Power Point Deliverable Methods : Agile and Devops