This topic is locked

sql query and asp datetime

6/17/2004 5:27:16 AM
ASPRunnerPro General questions
T
Toad author

I am trying to get all the records from table1 where datetime is between 9:00 AM yesterday and 9:00 AM today if time now is less than 9:00 am today. Otherwise return all where datetime > 9:00 AM today.
I do not beleive this can be solved with a sql query and so I am hoping that it can be done with asp. I have no experience with asp so any suggestions are welcome.

Sergey Kornilov admin 6/18/2004

If you use MS Access you can use IIF and DatePart functions to build SQL query that you need.
DatePart function can help you to extract hour portion of current time.
IIF function allows you to execute one of two expressions based on some condition (time now is less than 9:00 am).
You need to read MS Access help to get an idea how those functions work. Feel free to ask any questions if you need more help on this.

T
Toad author 6/18/2004

Unfortunately I am using sql2000. Oracle have the trunc function which would work perfectly but sql has no such function so my only option was if it is possible in ASP.
date_to_test between (trunc(sysdate-1)+9/24) and (trunc(sysdate)+9/24)
I will however check on IIF and DatePart to see if they can help.

T
Toad author 6/18/2004

Here are two solutions: <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=2119&image=1&table=forumreplies' class='bbc_emoticon' alt=':D' />

  1. Provided by Erland Sommarskog
    SELECT *

    FROM Orders

    WHERE (DATEPART(hour, GETDATE()) >= 9) AND (PlaceOrderDateTime >= CONVERT(char(8), GETDATE(), 112) + ' 09:00:00') OR

    (DATEPART(hour, GETDATE()) < 9) AND (PlaceOrderDateTime BETWEEN DATEDIFF(day, - 1, CONVERT(char(8), GETDATE(), 112) + ' 09:00:00') AND

    CONVERT(char(8), GETDATE(), 112) + ' 09:00:00')
  2. Provided by John Gilson
    SELECT

    FROM Orders

    WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) < 9 AND PLaceORderDateTime BETWEEN DATEADD(HOUR, 9, CONVERT(CHAR(8),

    CURRENT_TIMESTAMP - 1, 112)) AND DATEADD(HOUR, 9, CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112))

    UNION ALL

    SELECT


    FROM Orders

    WHERE DATEPART(HOUR, CURRENT_TIMESTAMP) >= 9 AND PLaceORderDateTime > DATEADD(HOUR, 9, CONVERT(CHAR(8), CURRENT_TIMESTAMP, 112))