We have simple problem.
A MS SQL stored procedure will be executed any day arround 1:0am (+/- 10 minutes). The stored procedure will perform simple math and statistic on data from particular table. For simplicity let us assume that it will calculate average value of record accumulated in a table.
The average value will be calculated for data in predetermined time frame : from 0:01am till 24:59pm for the previous day (in essence average for this values for yesterday).
Since we do not know when exactly the SQL query will run, we do not know the time different between midnight and the time when the SQL query runs.
The questions is: how can we refer to yesterday day as start time and end time?
My existing query is:
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DateAdd(hh,-24,GetDate())
SET @EndDate = GetDate()
After this line the query continues with actual data extraction and manipulation - finding average.
The @StartDate is already set to Midnight at two days ago ( the midnight before the last one). The @EndDate shall be the time of the most recent Midnight.
Question: how do we set the @EndDate to be the time of the most recent Midnight??