I have data in 5-minute intervals with 24-hour roll-overs, like so:
LogTime
-------------
7/1/2017 7:01
7/1/2017 7:06
7/1/2017 7:11
7/1/2017 7:16
...
7/2/2017 6:56
I need a TSQL query to select all rows for a given date, e.g. entries from 07/02 00:00:00
through 07/02 06:59:59
are considered to be part of 07/01
's data.
I'm using this query in SSMS' editor pane:
SELECT * FROM [LogEntries] WHERE [LogTime] BETWEEN '20170701 06:59:59' AND '20170702 07:00'
The problem here is that the strings need to be logically constructed from DateTime
values.
Can this be done in a single query statement with passed-in DateTime
parameters? I'm doing this for an SSRS report and I'd rather not resort to a Stored Procedure if I don't have to.
The question states "The problem here is that the strings need to be logically constructed from DateTime values".
In the BETWEEN (Transact-sql) documentation cited by the OP, example D (for datetimes) says this:
D. Using BETWEEN with datetime values
The following example retrieves rows in which datetime values are between '20011212' and '20020105', inclusive.
SELECT BusinessEntityID, RateChangeDate
FROM HumanResources.EmployeePayHistory
WHERE RateChangeDate BETWEEN '20011212' AND '20020105';
This makes it seem that the DateTime values needs to be supplied as strings. They don't. You can use datetimes.
Zohar's answer addresses the time-shifting part of the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With