%CHAR (Convert to Character Data)

%CHAR(expression{:format})

%CHAR converts the value of the expression from graphic, UCS-2, numeric, date, time or timestamp data to type character. The converted value remains unchanged, but is returned in a format that is compatible with character data.

If the parameter is a constant, the conversion will be done at compile time.

If a UCS-2 conversion results in substitution characters, a warning message will be given in the compiler listing if the parameter is a constant. Otherwise, status 00050 will be set at run time but no error message will be given.

For graphic data, the value returned includes the shift-in and shift-out characters. For example, if a 5 character graphic field is converted, the returned value is 12 characters (10 bytes of graphic data plus the two shift characters). If the value of the expression has a variable length, the value returned is in varying format.

For date, time, or timestamp data, the second parameter contains the date, time, or timestamp format to which the returned character data is converted. The value returned will include separator characters unless the format specified is followed by a zero.

For numeric data, if the value of the expression is float, the result will be in float format (for example '+1.125000000000000E+020'). Otherwise, the result will be in decimal format with a leading negative sign if the value is negative, and without leading zeros. The character used for any decimal point will be the character indicated by the control specification DECEDIT keyword (default is '.'). For example, %CHAR of a packed(7,3) expression might return the value '-1.234'.

For more information, see Conversion Operations or Built-in Functions.

Figure 198. %CHAR Examples
 *..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
D Name            S             20G   VARYING INZ(G'oXXYYZZi')
D date            S               D   INZ(D'1997/02/03')
D time            S               T   INZ(T'12:23:34')
D result          S            100A   VARYING
D points          S             10i 0 INZ(234)

 *-----------------------------------------------------------------
 * To format the time and date with the default formats, use this:
 *-----------------------------------------------------------------
 /FREE
   result = 'It is ' + %CHAR(time) + ' on ' + %CHAR(date);
   // If the default formats are both *USA,
   // result = 'It is 12:23 PM on 02/03/1997'
 
   //-------------------------------------------------------------
   // To format the time and date with the job formats, use this:
   //-------------------------------------------------------------
   result = 'It is ' + %CHAR(time : *jobrun)
            + ' on ' + %CHAR(date : *jobrun);
   // If the job date format is *MDY- and the time separator is '.',
   // then the result = 'It is 12.23.34 on 97-02-03'
 
   //--------------------------------------------------------------
   // To format the time and date with specific formats, use this:
   //--------------------------------------------------------------
   result = 'It is ' + %CHAR(time : *hms:)
            + ' on ' + %CHAR(date : *iso);
   // result = 'It is 12:23:34 on 1997-02-03'
   //
 
   //-------------------------------------------------------------
   // You can use %subst with the %char result if you only want
   // part of the result
   //-------------------------------------------------------------
   result = 'The time is now ' + %SUBST (%CHAR(time):1:5) + '.';
   // result = 'The time is now 12:23.'
 
   //-------------------------------------------------------------
   // Use %CHAR to convert a graphic value to character so it
   // can be concatenated with a character value.
   //-------------------------------------------------------------
   result = 'The customer''s name is ' + %CHAR(Name) + '.';
   // result = 'The customer's name is oXXYYZZi.'
 
   //----------------------------------------------------
   // Use %CHAR to convert a number to character format:
   //----------------------------------------------------
   result = 'You have ' + %char(points) + ' points.';
   // result = 'You have 234 points.'
   //
 /END-FREE
Note:
The graphic literal in this example is not a valid graphic literal. See Graphic Format for more information.

 



[ Top of Page | Previous Page | Next Page | Contents | Index ]