views:

37

answers:

2

Hi

I want to get an entire date

So today would be 7/7/2010 12:00:00 am to 7/7/2010 11:59:59 pm

So that should be the full 24 hours since 12:00:00 am would be the 8th then.

So I have this

select DATEADD(??, ??, DATEDIFF(dd, 0, GETUTCDATE()))

How do I make it add 23 hours 59mins and 59seconds to it?

+3  A: 

Try this:

DATEADD(second, -1, DATEADD(DAY, 1,"7/7/2010 12:00:00"))

Byron Whitlock
Hmm I can't have it hard coded of course. So I took what you had and made this it seems to work but then again I don't know what I really didDATEADD(dd, 0, DATEDIFF(dd, 0, GETUTCDATE())) AND DATEADD(ss, -1, DATEDIFF(dd, -1, GETUTCDATE())
chobo2
@chobo2: There's a tiny chance of a race condition with the code in your comment - if the date ticks over midnight between the first statement and the second then you'll end up with a range that covers two days rather than one.
LukeH
+1  A: 
DECLARE @start DATETIME
DECLARE @end DATETIME

SET @start = DATEADD(dd, 0, DATEDIFF(dd, 0, GETUTCDATE()))
SET @end = DATEADD(dd, 1, DATEADD(ms, -3, @start))
LukeH
fancy bastard using millisecond precision ;) But really, good clear answer +1
Byron Whitlock
Is this better? Will it encounter this race condition?
chobo2
@chobo2: This calculates `@end` in relation to `@start` rather than by calling `GETUTCDATE()` a second time, so it's not vulnerable to the race condition. In the (unlikely) event that the date ticks over midnight between the two statements then the range will still be a single day.
LukeH