DATECVT: Converting the Format of a Date

How to:

The DATECVT function converts the format of a date in an application without requiring an intermediate calculation. 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.


Top of page

x
Syntax: How to Convert a Date Format
DATECVT(date, 'infmt', {'outfmt'|outfield})

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.

infmt

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 infmt functions as an offset from the base date of a YYMD field (12/31/1900).
outfmt

Alphanumeric

Is the output format 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 infmt functions as an offset from the base date of a YYMD field (12/31/1900).
outfield

Format must match the format specified for the output format (outfmt).

Is the name of the field that contains the result.



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

Information Builders