April 30, 2013 at 4:22 am
Hi All,
Sorry if this has been asked somewhere all ready but I've struggled to find the answer!
I have a date time field which is a date time stamp on activity. i need to have a query that only brings back information for data that's between today's date and events between 08:00 and 10:00, but i need the date to be dynamic, so that it always returns today's date with that between those times.
My brains says it should be straight forward but i cant suss it.
Thanks
April 30, 2013 at 4:32 am
Can you work it out from this:
SELECT
d.Today,
StartTime = DATEADD(hour,8,d.Today),
EndTime = DATEADD(hour,10,d.Today)
FROM (SELECT Today = CAST(CAST(GETDATE() AS DATE) AS DATETIME)) d
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
April 30, 2013 at 4:40 am
Here is one way of doing it:
select Col1,Col2
from MyTable
where DateCol > dateadd(hh,8,(dateadd(dd,datediff(dd,'20100101',getdate()),'20100101')))
and DateCol < dateadd(hh,10,(dateadd(dd,datediff(dd,'20100101',getdate()),'20100101')))
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply