Select Data Base on DATE and TIME Range

  • Hello Expert,

    I would like to select the data base on the DATE and TIME range. For example: OrderDate between 12/5/2007 5:30pm and 12/6/2007 5:30pm. Basically, the users only want to see the orders created between 5:30pm 12/5 and 5:30pm 12/6. The OrderDate field date type is DATETIME. I'm on SQL Server 2005. Your help/suggestion is greatly appreciated. Thanks

  • Have you tried:

    where

    orderDate between '12/5/2007 5:30pm' and '12/6/2007 5:30pm'

    you can also use a CAST to make sure it goes into a datetime, but the syntax above will do the CAST anyway...

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • Thank you for your response. Here is my task:

    I'm passing the date parameters into a stored proc. When the users run the report, they provide the date range. So if they pass in 12/5/2007 and 12/6/2007, I want to translate it into 12/5/2007 5:30pm and 12/6/2007 5:30pm.

  • Assuming you're passing the parameters as strings, you could try...

    create proc myproc(@startdatestr as varchar(20),@enddatestr as varchar(20))

    as

    begin

    declare @startdate datetime

    declare @enddate datetime

    select

    @startdate=cast(@startdatestr+' 05:30pm' as datetime),

    @enddate=cast(@enddatestr +' 05:30pm' as datetime)

    select whatever

    from mytable

    where orderdate between @startdate and @enddate

    .....

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply