CONVERSION in COBOL refers to the process of transforming data from one format, type, or representation to another. This includes converting between different data types, formats, and representations to ensure compatibility and proper data processing in COBOL applications.
CONVERSION operations follow specific syntax patterns in COBOL:
123456789101112131415161718192021222324252627282930* Basic data conversion using MOVE 01 numeric-field PIC 9(5) VALUE 12345. 01 alphanumeric-field PIC X(10). 01 converted-field PIC 9(5). * Numeric to alphanumeric conversion MOVE numeric-field TO alphanumeric-field. * Alphanumeric to numeric conversion MOVE alphanumeric-field TO converted-field. * Using FUNCTION for conversions 01 date-string PIC X(10) VALUE "2024-01-15". 01 converted-date PIC 9(8). * Date conversion MOVE FUNCTION DATE-OF-INTEGER( FUNCTION INTEGER-OF-DATE(date-string) ) TO converted-date. * String case conversion 01 original-text PIC X(20) VALUE "Hello World". 01 upper-text PIC X(20). 01 lower-text PIC X(20). * Convert to uppercase MOVE FUNCTION UPPER-CASE(original-text) TO upper-text. * Convert to lowercase MOVE FUNCTION LOWER-CASE(original-text) TO lower-text.
CONVERSION uses MOVE statements and functions to transform data.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960* Advanced conversion operations PROCEDURE DIVISION. PERFORM-CONVERSIONS. * Currency conversion PERFORM convert-currency. * Date format conversion PERFORM convert-date-format. * String formatting conversion PERFORM convert-string-format. STOP RUN. CONVERT-CURRENCY. * Convert currency format 01 currency-amount PIC X(15) VALUE "$1,234.56". 01 clean-amount PIC X(15). 01 numeric-amount PIC 9(8)V99. * Remove currency symbols and formatting MOVE currency-amount TO clean-amount. INSPECT clean-amount REPLACING ALL "$" BY SPACE. INSPECT clean-amount REPLACING ALL "," BY SPACE. INSPECT clean-amount REMOVAL ALL SPACES. * Convert to numeric MOVE clean-amount TO numeric-amount. DISPLAY "Original: " currency-amount. DISPLAY "Converted: " numeric-amount. CONVERT-DATE-FORMAT. * Convert date format 01 input-date PIC X(10) VALUE "15/01/2024". 01 output-date PIC X(10). * Convert DD/MM/YYYY to YYYY-MM-DD MOVE input-date(7:4) TO output-date(1:4). MOVE "-" TO output-date(5:1). MOVE input-date(4:2) TO output-date(6:2). MOVE "-" TO output-date(8:1). MOVE input-date(1:2) TO output-date(9:2). DISPLAY "Input date: " input-date. DISPLAY "Output date: " output-date. CONVERT-STRING-FORMAT. * Convert string format 01 phone-string PIC X(15) VALUE "(555) 123-4567". 01 clean-phone PIC X(15). * Remove formatting characters MOVE phone-string TO clean-phone. INSPECT clean-phone REPLACING ALL "(" BY SPACE. INSPECT clean-phone REPLACING ALL ")" BY SPACE. INSPECT clean-phone REPLACING ALL "-" BY SPACE. INSPECT clean-phone REMOVAL ALL SPACES. DISPLAY "Original phone: " phone-string. DISPLAY "Clean phone: " clean-phone.
Here are some practical uses of CONVERSION in COBOL:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697* Data import and conversion system DATA DIVISION. WORKING-STORAGE SECTION. 01 imported-data. 05 raw-customer-id PIC X(10). 05 raw-customer-name PIC X(50). 05 raw-customer-age PIC X(3). 05 raw-customer-salary PIC X(15). 01 converted-data. 05 customer-id PIC 9(8). 05 customer-name PIC X(50). 05 customer-age PIC 9(3). 05 customer-salary PIC 9(8)V99. 01 conversion-errors. 05 error-count PIC 9(3) VALUE 0. 05 error-message PIC X(100). PROCEDURE DIVISION. CONVERT-IMPORTED-DATA. * Read imported data READ import-file AT END MOVE "Y" TO end-of-file-flag NOT AT END PERFORM convert-customer-data END-READ. * Display conversion summary DISPLAY "Conversion completed. Errors: " error-count. STOP RUN. CONVERT-CUSTOMER-DATA. * Convert customer ID (alphanumeric to numeric) IF raw-customer-id IS NUMERIC MOVE raw-customer-id TO customer-id ELSE ADD 1 TO error-count MOVE "Invalid customer ID format" TO error-message PERFORM log-conversion-error END-IF. * Convert customer name (trim and validate) MOVE raw-customer-name TO customer-name. INSPECT customer-name REMOVAL LEADING SPACES. INSPECT customer-name REMOVAL TRAILING SPACES. * Convert customer age (alphanumeric to numeric) IF raw-customer-age IS NUMERIC MOVE raw-customer-age TO customer-age IF customer-age < 0 OR customer-age > 150 ADD 1 TO error-count MOVE "Invalid age value" TO error-message PERFORM log-conversion-error END-IF ELSE ADD 1 TO error-count MOVE "Invalid age format" TO error-message PERFORM log-conversion-error END-IF. * Convert customer salary (currency to numeric) PERFORM convert-salary. * Write converted data if no errors IF error-count = 0 WRITE converted-record FROM converted-data END-IF. CONVERT-SALARY. * Convert salary from currency format to numeric MOVE raw-customer-salary TO temp-salary. * Remove currency symbols and formatting INSPECT temp-salary REPLACING ALL "$" BY SPACE. INSPECT temp-salary REPLACING ALL "," BY SPACE. INSPECT temp-salary REMOVAL ALL SPACES. * Validate and convert IF temp-salary IS NUMERIC MOVE temp-salary TO customer-salary IF customer-salary <= 0 ADD 1 TO error-count MOVE "Invalid salary value" TO error-message PERFORM log-conversion-error END-IF ELSE ADD 1 TO error-count MOVE "Invalid salary format" TO error-message PERFORM log-conversion-error END-IF. LOG-CONVERSION-ERROR. * Log conversion errors DISPLAY "Conversion error: " error-message. DISPLAY "Customer ID: " raw-customer-id. * Additional error logging logic here.
Data import and conversion system for processing external data.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970* Report format conversion system PROCEDURE DIVISION. CONVERT-REPORT-FORMAT. * Convert report data for different output formats PERFORM convert-for-csv-format. PERFORM convert-for-xml-format. PERFORM convert-for-json-format. STOP RUN. CONVERT-FOR-CSV-FORMAT. * Convert data for CSV output 01 csv-record PIC X(200). 01 csv-separator PIC X VALUE ",". * Build CSV record MOVE customer-id TO csv-record(1:10). MOVE csv-separator TO csv-record(11:1). MOVE customer-name TO csv-record(12:50). MOVE csv-separator TO csv-record(62:1). MOVE customer-age TO csv-record(63:3). MOVE csv-separator TO csv-record(66:1). MOVE customer-salary TO csv-record(67:10). * Write CSV record WRITE csv-record TO csv-file. DISPLAY "CSV record written: " csv-record. CONVERT-FOR-XML-FORMAT. * Convert data for XML output 01 xml-record PIC X(500). * Build XML record MOVE "
" TO xml-record(1:10). MOVE " " TO xml-record(134:11). * Write XML record WRITE xml-record TO xml-file. DISPLAY "XML record written: " xml-record. CONVERT-FOR-JSON-FORMAT. * Convert data for JSON output 01 json-record PIC X(300). * Build JSON record MOVE '{"customer_id":"' TO json-record(1:15). MOVE customer-id TO json-record(16:10). MOVE '","name":"' TO json-record(26:9). MOVE customer-name TO json-record(35:50). MOVE '","age":' TO json-record(85:7). MOVE customer-age TO json-record(92:3). MOVE ',"salary":' TO json-record(95:9). MOVE customer-salary TO json-record(104:10). MOVE '}' TO json-record(114:1). * Write JSON record WRITE json-record TO json-file. DISPLAY "JSON record written: " json-record." TO xml-record(11:4). MOVE customer-id TO xml-record(15:10). MOVE " " TO xml-record(25:5). MOVE "" TO xml-record(30:6). MOVE customer-name TO xml-record(36:50). MOVE " " TO xml-record(86:7). MOVE "" TO xml-record(93:5). MOVE customer-age TO xml-record(98:3). MOVE " " TO xml-record(101:6). MOVE "" TO xml-record(107:8). MOVE customer-salary TO xml-record(115:10). MOVE " " TO xml-record(125:9). MOVE "
Report format conversion for different output formats (CSV, XML, JSON).
1. What is CONVERSION in COBOL?
2. What types of conversions can be performed in COBOL?
3. What is the primary purpose of CONVERSION in COBOL?
4. How is CONVERSION typically performed in COBOL?
5. What is a common use of CONVERSION in COBOL?