Board index » delphi » Q: Comparing datetime Field

Q: Comparing datetime Field

I'm using Delphi Client/Server 2.01 and MS SQL Server 6.5.

What's the proper way to compare a timestamp field (type datetime) when
performing an update?

I have a stored procedure set up to do an update, followed by something
like "where name=@name and edt_dt=@edt_dt", but the datetime field
comparison always fails, and the update is never performed.  What's the
proper way to read in the initial value of the timestamp field and then
pass it to the stored procedure?  I currently just store it into a
TDateTime variable by setting it equal to
qryTest.FieldByName('edt_dt').AsDateTime and then setting the parameter
in the stored procedure to this value, but this never works.

Any help would be greatly appreciated.  Thanks.

 

Re:Q: Comparing datetime Field


TDateTime is a floating point number, where the integer part is the
number of days from the base, and the fractional part is the time as a
fractional day.

You can *never* compare two floats for equality.  The LSBs will almost
always be off.  

If you want to just check the date part of the DateTime, just compare
the integer portion.

If you need to compare the time portion as well, you need to decide
how close the need to be for your purpose.  Call this value epsilon.

var
epsilon: TDateTime
...
epsilon := 5/(24*60)    // 5 minutes
...
if (dt1 - dt2 < epsilon) then TheyAreEqual(within 5 minutes);

c...@netcom.com (Son of Westwood) wrote:

Quote
>I'm using Delphi Client/Server 2.01 and MS SQL Server 6.5.

>What's the proper way to compare a timestamp field (type datetime) when
>performing an update?

>I have a stored procedure set up to do an update, followed by something
>like "where name=@name and edt_dt=@edt_dt", but the datetime field
>comparison always fails, and the update is never performed.  What's the
>proper way to read in the initial value of the timestamp field and then
>pass it to the stored procedure?  I currently just store it into a
>TDateTime variable by setting it equal to
>qryTest.FieldByName('edt_dt').AsDateTime and then setting the parameter
>in the stored procedure to this value, but this never works.

>Any help would be greatly appreciated.  Thanks.

Other Threads