7.122.2 SUBSTRING Examples

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:

#A Before Substr.

#B Before Substr.

Substring Command

#B After Substr.

ABCDEFGHIJ

XXXXX

FIELD(#A) INTO_FIELD(#B)

ABCDE

ABCDEFGHIJ

XXXXX

FIELD(#A 1 1) INTO_FIELD(#B)

A

ABCDEFGHIJ

XXXXX

FIELD(#A 1 1) INTO_FIELD(#B 1 1)

AXXXX

ABCDEFGHIJ

XXXXX

FIELD(#A 2 2) INTO_FIELD(#B)

BC

ABCDEFGHIJ

XXXXX

FIELD(#A 2 2) INTO_FIELD(#B 2)

XBC

ABCDEFGHIJ

XXXXX

FIELD(#A 2 2) INTO_FIELD(#B 2 2)

XBCXX

ABCDEFGHIJ

XXXXX

FIELD(#A 9 1) INTO_FIELD(#B)

I

ABCDEFGHIJ

XXXXX

FIELD(#A 9 2) INTO_FIELD(#B)

IJ

ABCDEFGHIJ

XXXXX

FIELD(#A 9 2) INTO_FIELD(#B 2)

XIJ

ABCDEFGHIJ

XXXXX

FIELD(#A 9 2) INTO_FIELD(#B 2 2)

XIJXX

 

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