Example 1: If field #A is alphanumeric length 10 and field #B is alphanumeric length 5 then the following table indicates what happens when various SUBSTRING commands are used:
|
Example 2: Use the SUBSTRING command to alter a numeric six date field called #DDMMYY from format DDMMYY to YYMMDD:
DEFINE FIELD(#WORK02) TYPE(*CHAR) LENGTH(2)
SUBSTRING FIELD(#DDMMYY 1 2) INTO_FIELD(#WORK02)
SUBSTRING FIELD(#DDMMYY 5 2) INTO_FIELD(#DDMMYY 1 2)
SUBSTRING FIELD(#WORK02) INTO_FIELD(#DDMMYY 5 2)
Example 3: The following RDML program stores up to 20 product numbers input by the user in one long string called #PRODUCTS, then prints them all when no more are entered:
DEFINE FIELD(#PRODUCTS) TYPE(*CHAR) LENGTH(200)
DEFINE FIELD(#ENTERED) TYPE(*DEC) LENGTH(3) DECIMALS(0) DEFAULT(0)
DEFINE FIELD(#I) TYPE(*DEC) LENGTH(3) DECIMALS(0)
DOUNTIL '(#PRODNO = *BLANKS) *OR (#ENTERED = 20)'
CHANGE #PRODNO *BLANKS
REQUEST FIELDS(#PRODNO)
IF '#PRODNO *NE *BLANKS'
CHANGE #ENTERED ('#ENTERED + 1')
CHANGE #I '((#ENTERED - 1) * 10) + 1'
SUBSTRING FIELD(#PRODNO) INTO_FIELD(#PRODUCTS #I 10)
ENDIF
ENDUNTIL
DOWHILE '#ENTERED *GT 0'
CHANGE #I '((#ENTERED - 1) * 10) + 1'
SUBSTRING FIELD(#PRODUCTS #I 10) INTO_FIELD(#PRODNO)
FETCH FIELDS(#DESCRIPT #PRICE #QUANTITY) FROM_FILE(PRODMAST) WITH_KEY(#PRODNO)
UPRINT FIELDS(#PRODNO #DESCRIPT #PRICE #QUANTITY)
CHANGE #ENTERED ('#ENTERED - 1')
ENDWHILE