%CHAR(date|time|timestamp {: format})

Start of change%CHAR can convert the value of a date, time, or timestamp expression to character.End of change

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

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.

Start of changeThe CCSID of the returned value is *JOBRUN.End of change

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

%CHAR Examples with Date, Time, and Timestamp parameters

Start of change
    DCL-S date DATE INZ(D'1997/02/03');
    DCL-S time TIME INZ(T'12:23:34');
    DCL-S timestamp6 TIMESTAMP INZ(Z'1997-02-03-12.45.59.123456');
    DCL-S timestamp0 TIMESTAMP(0) INZ(Z'1997-02-03-12.45.59');
    DCL-S timestamp1 TIMESTAMP(1) INZ(Z'1997-02-03-12.45.59.1');
    DCL-S result VARCHAR(100);
End of change
This example formats the time and date with the default formats. Assume that the default formats are both *USA.
   result = 'It is ' + %CHAR(time) + ' on ' + %CHAR(date);
   // result = 'It is 12:23 PM on 02/03/1997'
This example formats the time and date with the job formats. Assume that the job date format is *MDY- and the job time separator is a period.
   result = 'It is ' + %CHAR(time : *jobrun)
          + ' on ' + %CHAR(date : *jobrun);
   // result = 'It is 12.23.34 on 97-02-03'
This example formats the time and date with specific formats.
   result = 'It is ' + %CHAR(time : *hms:)
          + ' on ' + %CHAR(date : *iso);
   // result = 'It is 12:23:34 on 1997-02-03'
Start of changeThis example formats the timestamps with separators:
   result = %CHAR(timestamp0);
   // result = '1997-02-03-12.45.59'
   result = %CHAR(timestamp1);
   // result = '1997-02-03-12.45.59.1'
   result = %CHAR(timestamp6);
   // result = '1997-02-03-12.45.59.123456'
End of change
Start of changeThis example formats the timestamps with no separators by specifying the '&' separator:
   result = %CHAR(timestamp0 : *iso&);
   // result = '19970203124559'
   result = %CHAR(timestamp1 : *iso&);
   // result = '199702031245591'
   result = %CHAR(timestamp6 : *iso&);
   // result = '19970203124559123456'
End of change
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.'