Solved

How to measure time since midnight in SQL query

Posted on 2013-01-16
4
1,019 Views
Last Modified: 2013-01-16
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??
0
Comment
Question by:uripapuk
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Steve Wales
ID: 38784615
Most recent midnight:

declare @EndDate datetime
set @EndDate = dateadd(dd, datediff(dd, 0, getdate())+0, 0)

If you want 23:59:59 of the one second before that you can subtract a second:

set @EndDate = dateadd(ss, -1, @EndDate)
0
 

Author Comment

by:uripapuk
ID: 38784679
This solution seems to be correct.
I have one clarifying question.

Since my statement is "SET @StartDate = DateAdd(hh,-24,GetDate())" does this means that am extracting only the date portion of the date OR does this means that I am only making a time shift in units of hours from the precise current time (mm,dd,yy) ??

What I like to make sure is that my EndDate is most recent midnight with precision up to 1 second AND my StartDate is the time of the midnight before the last one (midnight a day ago) with precision up to 1 second.

Would you please clarify.
0
 
LVL 22

Accepted Solution

by:
Steve Wales earned 400 total points
ID: 38784729
What you're doing is substracting 24 hours from the current date/time with your statement.

If it runs at 01:05:00 AM, then you get 24 hours less than that.

If you want from mm/dd/yy 00:00:00 to mm/dd/yy 23:59:59 then you want to use something like this:

Your start date, if you want Midnight of 2 days ago:

 -- Yesterday at midnight (start of day)
SELECT @Start = dateadd(dd, datediff(dd, 0, dateadd(dd, -1, getdate()))+0, 0)

Open in new window

Then you want this for 23:59:59 last night:

set @EndDate = dateadd(dd, datediff(dd, 0, getdate())+0, 0)
set @EndDate = dateadd(ss, -1, @EndDate)

Open in new window

0
 

Author Closing Comment

by:uripapuk
ID: 38785050
The expert responded promptly and in detail on all questions asked. The solution proposed was tested and confirmed working as required.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ORA-00923: FROM keyword not found where expected 3 60
SSRS 2013 - Overlapping reports 2 19
Help Required 3 91
Query group by data in SQL Server - cursor? 3 29
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question