ASP DATE between NOW and 24 HOURS earlier.

D

Darren

Hello,

How would I write in SQL or ASP the following?:
Count number of Database Records between NOW and 24 HOURS EARLIER? I know
how to retrieve recordsets etc - its just the date thing I cant grasp. I am
stuck getting the 24 hours earlier data capture.

Thanks!

Gary.
PS - Sorry for the X-Posting. Just need to answer fairly quickly if at all
possible. Hopefully relevant to all groups!
 
J

Jon Davis

Try this C#:

string yesterday = DateTime.Now.AddDays(-1).ToString();
string now = DateTime.Now.ToString();
string sSQL = "SELECT * FROM FIELD "
+ "WHERE DATE > '" + yesterday + "' AND DATE <= '" + now + "'";

Untested; might be a minor syntax typo but your compiler should catch it.
For instance, I don't remember if dates are 'date' or %date%.
 
B

Bob Barrows

Darren said:
Hello,

How would I write in SQL or ASP the following?:
Count number of Database Records between NOW and 24 HOURS EARLIER? I
know how to retrieve recordsets etc - its just the date thing I cant
grasp. I am stuck getting the 24 hours earlier data capture.

Thanks!

Gary.
PS - Sorry for the X-Posting. Just need to answer fairly quickly if
at all possible. Hopefully relevant to all groups!

Look, this is a asp and database question. Unless you are using .Net, there
is ONE relevant group for this question: .inetserver.asp.db. Cross-posting
to thousands of semi-related groups will NOT get you a quicker answer.

I would set follow-ups to .inetserver.asp.db if your inclusion of the
dotnet groups hadn't raised the doubt that you might be using .Net.

Why don't you post back to the relevant newsgroup and let us know what
technology you are using (asp or asp.net) AND what type and version of
database you are using (I know you said "SQL", but you could have been
talking about the language, instead of SQL Server.)

When you reply, please set Follow-ups to the relevant newsgroup.

Bob Barrows
 
J

Jos

Jon said:
Try this C#:

string yesterday = DateTime.Now.AddDays(-1).ToString();
string now = DateTime.Now.ToString();
string sSQL = "SELECT * FROM FIELD "
+ "WHERE DATE > '" + yesterday + "' AND DATE <= '" + now +
"'";

Untested; might be a minor syntax typo but your compiler should catch
it. For instance, I don't remember if dates are 'date' or %date%.

Also note that this code will only work on clients configured for
the American English region.
If the client is configured for another region, than
now and yesterday will be formatted accordingly,
while the SQL string always needs to be in the mm-dd-yy format.

Therefore, it is better to use parameters in the SQL string:

string sSQL = "SELECT * FROM FIELD " +
"WHERE DATE > @date1 AND DATE <= @date2;"
SqlCommand cm = new SqlCommand(sSQL, conn);
cm.Parameters.Add("@date1",yesterday);
cm.Parameters.Add("@date2",now);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top