Xbasic

CDATE Function

Syntax

C CDATE(D date)

Formatted_Date as C = CDATE( date as D )

Formatted_Date as C = CDATE( date_time as T )

Arguments

date

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

date-time

A variable containing a date time value.

Description

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

Discussion

CDATE() interprets a Date value and returns 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 CDATE() function order dates chronologically (year, then month, then day), while those containing DTOC() order dates by month, day, and year.

Example

? cdate(date() )
= "20050617"
? cdate({6/15/05})
= "20050615"
INV_DATE = {12/18/1993}
? cdate(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 + cdate(START)

The following table shows the order created by this index:

EMPLOYEE
>
FIRSTNAME
DEPT and START DATE
Steve

Marketing, 03/21/78

Kate

Marketing, 04/17/79

Sarah

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 CDATE() , the following ordering results:

EMPLOYEE
>
FIRSTNAME
DEPT and START DATE
Sarah

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

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:

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

If you want to test for a blank date, use the following expression:

if (trim(cdate(datevalue) ) = "") then
    ' blank value code
else
    ' valid date code
end if

See Also