DATECVT: Converting the Format of a Date

How to:

Available Languages: reporting, Maintain

The DATECVT function converts the field value of any standard date format or legacy date format into a date format (offset from the base date), in the desired standard date format or legacy date format. If you supply an invalid format, DATECVT returns a zero or a blank.

DATECVT turns off optimization and compilation.

Note: You can use simple assignment instead of calling this function.

Syntax: How to Convert a Date Format

DATECVT(date, 'in_format', output)

where:

date

Date

Is the date to be converted. If you supply an invalid date, DATECVT returns zero. When the conversion is performed, a legacy date obeys any DEFCENT and YRTHRESH parameter settings supplied for that field.

in_format

Alphanumeric

Is the format of the date enclosed in single quotation marks. It is one of the following:

  • A non-legacy date format (for example, YYMD, YQ, M, DMY, JUL).
  • A legacy date format (for example, I6YMD or A8MDYY).
  • A non-date format (such as I8 or A6). A non-date format in in_format functions as an offset from the base date of a YYMD field (12/31/1900).
output

Alphanumeric

Is the output format enclosed in single quotation marks or a field containing the format. It is one of the following:

  • A non-legacy date format (for example, YYMD, YQ, M, DMY, JUL).
  • A legacy date format (for example, I6YMD or A8MDYY).
  • A non-date format (such as I8 or A6). This format type causes DATECVT to convert the date into a full component date and return it as a whole number in the format provided.

Example: Converting a YYMD Date to DMY

DATECVT converts 19991231 to 311299 and stores the result in CONV_FIELD:

CONV_FIELD/DMY = DATECVT(19991231, 'I8YYMD', 'DMY');

or

ONV_FIELD/DMY = DATECVT('19991231', 'A8YYMD', 'DMY');

Example: Converting a Legacy Date to Date Format (Reporting)

DATECVT converts HIRE_DATE from I6YMD legacy date format to YYMD date format:

TABLE FILE EMPLOYEE
PRINT FIRST_NAME AND HIRE_DATE AND COMPUTE
NEW_HIRE_DATE/YYMD = DATECVT(HIRE_DATE, 'I6YMD', 'YYMD');
BY LAST_NAME
WHERE DEPARTMENT EQ 'MIS';
END

The output is:

LAST_NAME     FIRST_NAME   HIRE_DATE  NEW_HIRE_DATE
---------     ----------   ---------  -------------
BLACKWOOD     ROSEMARIE    82/04/01   1982/04/01
CROSS         BARBARA      81/11/02   1981/11/02
GREENSPAN     MARY         82/04/01   1982/04/01
JONES         DIANE        82/05/01   1982/05/01
MCCOY         JOHN         81/07/01   1981/07/01
SMITH         MARY         81/07/01   1981/07/01