How do you Query only the DATE part of a DATETIME in WHERE C

Posted by JonathanWilson on 28-Apr-2008 07:46

Ok, now I know the following will work; but is really slow as it doesn't seem to use an index.

FOR EACH tblName WHERE

DATE(tblName.MyTimeStamp) = TODAY

NO-LOCK:

/Do Stuff/

END.

Then there's this way which is fast but tedious.

DEF VAR vt_date AS DATETIME NO-UNDO.

DEF VAR vd_date AS DATE NO-UNDO.

DEF VAR vt_start AS DATETIME NO-UNDO.

DEF VAR vt_end AS DATETIME NO-UNDO.

ASSIGN

vd_date = DATE(1,1,1980)

vt_date = DATETIME(DATE(1,1,1980), TIME * 100)

.

ASSIGN

vt_start = DATETIME(vd_date, 0)

vt_end = DATETIME(vd_date + 1, 0) - 1

.

FOR EACH container WHERE

(container.t_ship >= vt_start AND container.t_ship <= vt_end)

NO-LOCK:

DISP container.

END.

I've even tried something like:

FOR EACH container WHERE

t_ship BEGINS vd_date

NO-LOCK:

DISP container.

END.

As it would seem logical that you could do a fast lookup on a DATETIME variable and not have to also include a separate DATE field.

What am I missing.

All Replies

Posted by Thomas Mercer-Hursh on 28-Apr-2008 11:04

In the SQL world, where datetime has been around a lot longer, the > day and

Posted by jmls on 29-Apr-2008 04:33

much shorter version:

DEF VAR vd_date AS DATE NO-UNDO.

ASSIGN vd_date = 1/1/1980.

FOR EACH container WHERE

(container.t_ship GE DATETIME(vd_date) AND container.t_ship LT DATETIME(vd_date + 1) NO-LOCK:

DISP container.

END.

This thread is closed