Sql date compare query

Below is some sample table data from tblWagers

wager        StartDateTime   (stored as datetime)                    

400           2013-09-06 13:29:35.000          

200           2013-09-06 07:29:35.000      

1520         2013-09-07 00:59:35.000        

500           2013-09-07 01:01:35.000      

I am trying to write a query that will return all records with a startdatetime on or after 8am on the current date up to and including any records with a startdatetime  of 1 am  or earlier the following day

(For the purposes of this example, let's assume current date is 09/06/2013.  Given that,  the query is looking for 8am on 9/16/2013 to 1am on 9/17/2013)

So in the example above I would expect the query to return


400           2013-09-06 13:29:35.000    

1520         2013-09-07 00:59:35.000        

Not sure how to format where clause for correct date compare
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Give this a whirl in SSMS, then once you verify it works modify to meet your needs.

btw Which casino?
CREATE TABLE #tmp (wager int, StartDateTime datetime)
INSERT INTO #tmp (wager, StartDateTime)
	(400, '2013-09-11 13:29:35.000'),           
	(200, '2013-09-11 07:29:35.000'),     
	(1520, '2013-09-12 00:59:35.000'),       
	(500, '2013-09-12 01:01:35.000')  

SELECT wager, StartDateTime
FROM #tmp
WHERE StartDateTime BETWEEN CAST(CAST(CAST(GETDATE() as date) as varchar(10)) + ' 08:00:00.000' as datetime)
	and CAST(CAST(CAST(DATEADD(d, 1, GETDATE()) as date) as varchar(10)) + ' 01:00:00.000' as datetime)

Open in new window

Guessing there is a more elegant way to pull this off without all the CASTs, but none come to mind.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
These results:
400	September, 06 2013 13:29:35+0000
1520	September, 07 2013 00:59:35+0000

Open in new window

Produced by:
-- truncate time on getdate()
-- dateadd(day, datediff(day,0, <<data-here>> ), 0)

declare @s datetime, @f datetime
set @s = dateadd(hour, 8, dateadd(day, datediff(day,0, getdate() ), 0) )
set @f = dateadd(hour, 17, @s)

from yourtable
where ( StartDateTime >= @s and StartDateTime < @f )

from yourtable
where ( StartDateTime >= dateadd(hour, 8, dateadd(day, datediff(day,0, getdate() ), 0) )
    and StartDateTime < dateadd(hour, 25, dateadd(day, datediff(day,0, getdate() ), 0) )

Open in new window

is <date> 01:00:00 included in the results?
if it is then
change <
to <=
in the queries above

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Thanks for the grade.  Good luck with your project.  -Jim
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.