Solved

SQL DateTime Comparison

Posted on 2000-04-06
11
212 Views
Last Modified: 2010-04-04
I want to select records with a time stamp of (now - n*hours).  I'm using D3 and Paradox tables. Here's what I've done that doesn't work:

q is tquery
lastplay is tdatetime
n is integer

 q.Close;
 q.SQL.Clear;
 q.SQL.Add('select * from tracks where lastplay < :param');

 q.ParamByName('param').AsDateTime := now - n/24;
 q.Open;

what am i missing? it must be something simple.
0
Comment
Question by:rwv
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
? rwv,

where is the problem?
have done just a test and it works properly

sample code

//query holds : select * from time_db where dt < :param
procedure TForm1.Button1Click(Sender: TObject);
begin
  query1.Close;
  query1.ParamByName('param').AsDateTime := now - (strtoint(edit1.text)/24);
  query1.Open;
end;

do you get an errormessage?

meikl
0
 
LVL 6

Expert Comment

by:wimmeyvaert
Comment Utility
Hi rwv,

What error do you get ?

Regards,
The Mayor
0
 
LVL 2

Expert Comment

by:Felixin
Comment Utility
Hi, RWY and all of you


Are you sure that the conversion to hours of N/24 is OK?

Why don't you use

function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;

calling it like this

EncodeTime (MyNumberOFHours, 0, 0, 0);

The problem is that I don't understand what you mean by "It doesn't work". Do you have any kind of error message or, simply, you don't get the result that you where expecting?

Best regards
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
felixin
n/24 is ok
meikl
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
listening...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:rwv
Comment Utility
no error, i'm just not getting the result i expect.
my sql statement is a little more complex.  i'll look it over more closely and let you know.
0
 
LVL 9

Expert Comment

by:ITugay
Comment Utility
rwv,
can you show us your SQL and table structures? It will easy to help you.
-----
Igor.
0
 

Author Comment

by:rwv
Comment Utility
I've simplified my query and it still isnt working right.  

I'm using the var tn just to look at the values at a break point, they seem to be ok.

I've discovered something curious:
When set the value of the table's field (lastplay) to now() with an update query, the select query fails to exclude this record.
However, when I edit this record in a dbgrid, by only retyping a character of the date string, and then post it, the query works as expected.

Any ideas?

The only field below that really matters is lastplay which is a tDateTime.

function TfrmJBox.TrackQuery(cdid : integer) : boolean;
var ok : boolean;
    qwhere : string;
    tn : tdatetime;
begin
  ok := true;

    qwhere := 'where (cdid ='+inttostr(cdid)+') and ((lastplay < :dtlastplay) or (lastplay is null)) ';

  try
    if qTracks.State <> dsInactive then
      qTracks.Close;
    qTracks.SQL.Clear;
    qTracks.SQL.Add('select '+
                   'trackno, tracklen, title, votes, points, plays, lastplay, id, artist '+
                   'from "'+gDataDir+'\tracks.db" '+
                   qwhere+
                   'order by trackno');
    tn := now;
    qTracks.ParamByName('dtlastplay').AsDateTime := tn - gMinReplayTime/24;
    qTracks.Open;
  except
    ok := false;
  end;
  TrackQuery := ok;
end;
0
 
LVL 9

Accepted Solution

by:
ITugay earned 200 total points
Comment Utility
Still thinking...
but one crazy idea, is it possible you have different datetime format while making update by update query(dd/mm/yy and mm/dd/yy)?
----
Igor.
0
 

Author Comment

by:rwv
Comment Utility
Igor,
I didnt change datetime format, but you did lead me to the solution:
I was using
  ' set lastplay = "'+DateTimeToStr(now)+'"'
changed it to
  ' set lastplay = :paramNow'
then AddParamByName('paramNow').AsDateTime

However, I dont understand why this makes a difference.  Aren't Tdatetime types just numbers once they are stored?

Thanks all for your help to everyone.

Bobby Valentine
0
 

Author Comment

by:rwv
Comment Utility
correction
' set lastplay = :paramNow'
ParamByName('paramNow').AsDateTime := now;


 
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now