Xbasic

DTOS Function

Syntax

Formatted_Date as C = DTOS(Date_Time_Value as D)

Arguments

Date_Time_ValueDate

A variable containing a date value or a character string containing a legitimate representation of a date value (ISDATE(Date_Value) = .T.).

Returns

Formatted_DateCharacter

The Date_Value formatted into a character string.

Description

Converts a date into a character value in the form 'YYYYMMDD'.

Discussion

DTOS() converts a Date_Time_Value to a Formatted_Date character string in the form "YYYYMMDD." This function is useful for constructing multi-level indexes that contain both character and date fields.

Indexes containing the DTOS() function order dates chronologically (year, then month, then day), while those containing DTOC() order dates by month, day, and year.

Examples:

INV_DATE = {12/18/1993}
? dtos(INV_DATE)
= "19931218"

Assume you want to order an employee table by department and within each department by seniority. The table has a character field, DEPT, and a date field, STARTDATE. Use the following index expression:

DEPT + dtos(START)

The following Employee table shows the order created by this index:

FIRSTNAME

DEPT

START

Steve

Marketing

03/21/78

Kate

Marketing

04/17/79

Janice

Marketing

01/02/83

Ralph

R&D

01/01/80

Tim

R&D

03/20/80

Francie

R&D

02/17/84

Compare this result with the DTOC() function. If the DTOC() function is used in the above index expression instead of DTOS(), the following ordering results:

FIRSTNAME

DEPT

START

Janice

Marketing

01/02/83

Steve

Marketing

03/21/78

Kate

Marketing

04/17/79

Ralph

R&D

01/01/80

Francie

R&D

02/17/84

Tim

R&D

03/20/80

Entering search criteria to search on a date field presents a problem because you cannot type in a date directly as a text string. You must either surround the text you enter with the CTOD() function, or use a function to convert the date field to a character field before comparing it with the text.

If you want to search a date field, BIRTHDAY, for the date, "September 20, 1966," you can enter the following criteria:

dtos({09/20/66}) -> 19660920
dtos(BIRTHDAY) = "19660920" or
BIRTHDAY = ctod("09/20/1966") or
BIRTHDAY = ctod("09/20/66") or
BIRTHDAY = {9/20/66}

See Also