ASP DATE between NOW and 24 HOURS earlier.

Discussion in 'ASP .Net' started by Darren, Jan 26, 2004.

  1. Darren

    Darren Guest

    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!
     
    Darren, Jan 26, 2004
    #1
    1. Advertising

  2. Darren

    Jon Davis Guest

    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%.


    "Darren" <> wrote in message
    news:bd_Qb.9794$...
    > 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!
    >
    >
     
    Jon Davis, Jan 26, 2004
    #2
    1. Advertising

  3. Darren

    Bob Barrows Guest

    Darren wrote:
    > 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


    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows, Jan 26, 2004
    #3
  4. Darren

    Jos Guest

    Jon Davis wrote:
    > 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);

    --

    Jos Branders
     
    Jos, Jan 26, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rahmi Acar
    Replies:
    0
    Views:
    573
    Rahmi Acar
    Jul 17, 2003
  2. Darren

    ASP DATE between NOW and 24 HOURS earlier.

    Darren, Jan 26, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    201
  3. Darren
    Replies:
    3
    Views:
    190
  4. Edward
    Replies:
    3
    Views:
    194
    Gunnar Hjalmarsson
    Sep 8, 2004
  5. rutherf
    Replies:
    2
    Views:
    455
    rutherf
    Oct 28, 2006
Loading...

Share This Page