Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 38 ยท Data Manipulation

Reference Modification and Intrinsic Functions

Slice fields with COBOL reference modification (start:length) and use intrinsic functions: LENGTH, TRIM, UPPER-CASE, REVERSE, NUMVAL, CURRENT-DATE, INTEGER-OF-DATE, MOD, MAX and more.

Two final tools complete the data-manipulation toolbox. Reference modification takes a slice of a field by position, like a substring. Intrinsic functions (written FUNCTION NAME(arguments)) are built-in calculations for text, numbers and dates. Together they replace a lot of older, clumsier code built from INSPECT, REDEFINES and hand-written paragraphs.

Reference modification

COBOL
       01 PHONE        PIC X(12) VALUE "555-123-4567".
       ...
           DISPLAY PHONE(1:3)        *> 555
           DISPLAY PHONE(5:3)        *> 123
           DISPLAY PHONE(9:)         *> 4567   (from position 9 to the end)
           MOVE "XXX" TO PHONE(1:3)  *> modifies the first three characters

The form is name(start:length). Positions are 1-based, and the length is optional (the rest of the field is used). The slice is itself alphanumeric and can be used as a source or a receiver. Both numbers may be data items, which makes loops over characters easy:

COBOL
           PERFORM VARYING I FROM 1 BY 1 UNTIL I > 12
               IF PHONE(I:1) = "-"
                   ADD 1 TO DASH-COUNT
               END-IF
           END-PERFORM

A start or length outside the field is an error. Many compilers do not check it unless you ask (GnuCOBOL: cobc -debug), and a wrong value reads or writes memory next to your field, so check your subscripts. For a table element add the subscript first: NAME-TABLE(I)(1:3).

Python Reference modification (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Calling an intrinsic function

COBOL
           MOVE FUNCTION UPPER-CASE(NAME-IN) TO NAME-OUT
           COMPUTE ROOT = FUNCTION SQRT(25)
           DISPLAY FUNCTION LENGTH(NAME-IN)

Write FUNCTION name(argument ...). (A compiler directive or REPOSITORY. FUNCTION ALL INTRINSIC. lets you omit the word FUNCTION.) Arguments can be literals, data items or other functions. A function returns a value that you use like an expression.

Text functions

Function Returns
LENGTH(x) size of x in characters (bytes)
TRIM(x) x without leading and trailing spaces (TRIM(x LEADING) or TRAILING for one side)
UPPER-CASE(x), LOWER-CASE(x) case converted
REVERSE(x) characters in reverse order
ORD(c) position of a character in the collating sequence plus one (ORD("A") is 66 in ASCII)
CHAR(n) the character at position n (CHAR(66) is A)
CONCATENATE(a b ...) join (newer compilers)
COBOL
           MOVE FUNCTION TRIM(NAME-IN) TO NAME-OUT
           MOVE FUNCTION REVERSE(WORD) TO RESULT

TRIM returns a result whose length varies, so use it in a DISPLAY or STRING where its exact length matters.

Numeric functions

Function Returns
NUMVAL(x) number from text such as " -12.50 " (NUMVAL-C also accepts currency and commas)
INTEGER(x) greatest integer not greater than x (floor)
INTEGER-PART(x) integer part (truncation)
MOD(a, b) modulo with the sign of b: MOD(-7, 3) is 2
REM(a, b) remainder with the sign of a: REM(-7, 3) is -1
MAX(...), MIN(...), SUM(...), MEAN(...), MEDIAN(...) statistics over the arguments
SQRT(x), ABS(x), FACTORIAL(n), PI, E maths
RANDOM(seed) a pseudo-random number between 0 and 1
ANNUITY(rate, periods) payment factor for loans

NUMVAL is how you safely convert user-typed text to a number once IS NUMERIC is not enough (decimal points, signs and spaces are fine with NUMVAL).

Python Text and number functions (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Date functions

COBOL dates are mostly 8-digit numbers YYYYMMDD. Functions work with integer days, where day 1 is 1 January 1601:

Function Returns
CURRENT-DATE 21 characters: YYYYMMDDhhmmsshh then +hhmm or -hhmm (offset from GMT)
INTEGER-OF-DATE(yyyymmdd) day number
DATE-OF-INTEGER(n) the date yyyymmdd for a day number
INTEGER-OF-DAY, DAY-OF-INTEGER the same with yyyyddd (year and day of year)
WHEN-COMPILED the date and time the program was compiled
TEST-DATE-YYYYMMDD(d) 0 if valid, else the position of the first bad part

With them, date arithmetic is a subtraction:

COBOL
       01 NOW-TEXT          PIC X(21).
       01 START-DATE        PIC 9(8) VALUE 20260101.
       01 END-DATE          PIC 9(8) VALUE 20260315.
       01 DAYS-BETWEEN      PIC S9(5).
       ...
           MOVE FUNCTION CURRENT-DATE TO NOW-TEXT
           DISPLAY "Year: " NOW-TEXT(1:4)
           COMPUTE DAYS-BETWEEN =
               FUNCTION INTEGER-OF-DATE(END-DATE)
             - FUNCTION INTEGER-OF-DATE(START-DATE)

DAYS-BETWEEN is 73. To add 30 days to a date: FUNCTION DATE-OF-INTEGER(FUNCTION INTEGER-OF-DATE(D) + 30). For the weekday, FUNCTION MOD(FUNCTION INTEGER-OF-DATE(D), 7) gives 1 for Monday up to 6 for Saturday and 0 for Sunday, because 1 January 1601 was a Monday.

Python Date arithmetic (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting it together

A slice plus functions can parse a date text safely:

COBOL
       01 DATE-TEXT      PIC X(10) VALUE "2026-01-15".
       01 DATE-NUM       PIC 9(8).
       ...
           STRING DATE-TEXT(1:4) DATE-TEXT(6:2) DATE-TEXT(9:2)
               DELIMITED BY SIZE INTO DATE-NUM-TEXT
           MOVE FUNCTION NUMVAL(DATE-NUM-TEXT) TO DATE-NUM
           IF FUNCTION TEST-DATE-YYYYMMDD(DATE-NUM) NOT = 0
               DISPLAY "Invalid date"
           END-IF

Common mistakes

  • Treating positions as 0-based in FIELD(start:length).
  • A length of zero or a slice that ends past the field.
  • Forgetting that TRIM changes the length, so MOVE right after it pads again with spaces.
  • Passing an alphanumeric item with letters to NUMVAL and getting zero or garbage.
  • Confusing MOD and REM for negative numbers.
  • Assuming CURRENT-DATE is only a date; it returns date, time and offset.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does PHONE(5:3) return when PHONE is "555-123-4567"?

    Positions are 1-based: characters 5 to 7 of the text are 123.
  2. 2. What is FUNCTION INTEGER-OF-DATE(16010101)?

    Day numbers count from 1 January 1601, which is day 1.
  3. 3. Which function turns the text " -12.50 " into a number?

    NUMVAL converts text with spaces, a sign and a decimal point into a numeric value.
  4. 4. How many characters does FUNCTION CURRENT-DATE return?

    It returns 21 characters: 16 for date and time (to hundredths of a second) plus a 5-character offset from GMT.

Summary

  • field(start:length) slices a field using 1-based positions; it can be read or assigned.
  • FUNCTION name(args) gives text, numeric and date tools: TRIM, UPPER-CASE, REVERSE, NUMVAL, MOD, MAX, CURRENT-DATE, INTEGER-OF-DATE.
  • Date arithmetic is integer arithmetic on day numbers (day 1 is 1 January 1601).
  • Check ranges and conversions; MOD and REM differ for negative values.

Next: COBOL tutorial home