question

kmiura avatar image
kmiura asked

SQL - 24 hour window between dates

For example, I have a medication protocol that starts May 1, 2011 at 1PM and this medication is given 3 times within 24 hours then the dose changes, I want my next protocol to start on May 2, 2011 at 1:01 PM, I need the 24 hour window to reflect true time not restart at midnight. Any suggestions?
datetime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Pavel Pawlowski avatar image
Pavel Pawlowski answered
I think you are looking for the dateadd function. To add 24 hours to a DateTime value you simply add 1 day or 24 hours to the original value. DECLARE @date datetime SET @date = '2011/05/01 1PM' SELECT @date, DATEADD(day, 1, @date) as NexDate, DATEADD(hour, 24, @date) AS NextDate2 if you need to add another minute you can use DECLARE @date datetime SET @date = '2011/05/01 1PM' SELECT @date, DATEADD(minute, 1, DATEADD(day, 1, @date)) as NexDate, DATEADD(minute, 1, DATEADD(hour, 24, @date)) AS NextDate2 For details on the DATEADD function take a look on [MSDN DATEADD (Transact-SQL)][1] [1]: http://msdn.microsoft.com/en-us/library/ms186819.aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

kmiura avatar image kmiura commented ·
Thank you Pavel
0 Likes 0 ·
Tim avatar image
Tim answered
You should be able to use datediff by hours if you are wanting to compare two times. I am not absolutely sure what you are looking for. Can you give an example of how you are wanting this to look in a table or application?
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

kmiura avatar image kmiura commented ·
Well I am using a software application that needs to have a true 24hour window as the example above and they are telling me it can't be done. I am not a programmer so I was just inquiring to see if anyone could give me some more information. Thanks
0 Likes 0 ·
Tim avatar image Tim commented ·
Sure, just take a look at @Pavel Pawlowski response below. Your app guy should be able to account for this pretty easy either with SQL functions or with other app code.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.