Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Working with Time Frames in T-SQL Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, August 15, 2007 2:04 PM
 Forum Newbie Group: General Forum Members Last Login: Friday, June 28, 2013 8:25 AM Points: 2, Visits: 42
 Let's there's a process that begins at 1:00 and ends at 3:00. So 1:00 belongs to hour #1. 60 minutes later it is 2:00. But what about 2:00? it belongs to hour #2.So there are 59 minutes, 59 seconds in hour #1. The minimum time slice is a whole minute. There's no accommodation in the script for fractional minutes so those extra 59 seconds get tossed out. The same is true for a process that begins at 1:30 and ends at 2:00. There are actually 29 minutes, 29 seconds, 9999 milliseconds and so on consumed in hour 1. If you round the 59 seconds up to a whole minute then you risk counting more minutes in a day than actually exist, as the last minute in each hour would be counted twice, once at the end of the hour, and then again at the beginning of the next hour.The solution would be to account for fractional time slices.
Post #391068
 Posted Wednesday, August 15, 2007 3:41 PM
 SSCommitted Group: General Forum Members Last Login: Monday, December 09, 2013 10:02 AM Points: 1,588, Visits: 1,459
 There are not 59 minutes and 59 seconds in hour #1. There isn't even 59 minutes, 59.9999 seconds. There is 60 seconds in a minute and 60 minutes in an hour. Those are constants.If you were counting 2:00:00 as being part of hour #1, there would be 60 minutes and 1 second in hour #1.Test it for yourself. Count from 1:00:00 to 2:00:00 incrementing by 1 second.Or just run the following:Select Hour1 = DateDiff(mi, '1/1/2007 1:00 PM', '1/1/2007 2:00 PM'), Hour2 = DateDiff(mi, '1/1/2007 2:00 PM', '1/1/2007 3:00 PM'), Hour1and2Combined = DateDiff(mi, '1/1/2007 1:00 PM', '1/1/2007 3:00 PM') Or if that doesn't convince you, run the following. You will see that if you included 2:00 as part of hour #1, it would be the 61st minute, not the 60th.Declare @Start datetime, @End datetimeDeclare @Minutes Table (MinuteNumber int not null identity(1, 1) primary key, MyTime datetime not null)Select @Start = '1/1/2007 1:00 PM', @End = '1/1/2007 2:00 PM'While @Start <= @End    Begin        Insert Into @Minutes (MyTime)        Select @Start        Set @Start = DateAdd(mi, 1, @Start)    EndSelect *From @Minutes My blog: SQL SoldierTwitter: @SQLSoldierMy book: Pro SQL Server 2008 MirroringMicrosoft Certified Master: SQL Server 2008Principal DBA: Outerwall, Inc.Also available for consulting: SQL DBA Master
Post #391097
 Posted Wednesday, August 15, 2007 10:43 PM
 SSC-Dedicated Group: General Forum Members Last Login: Yesterday @ 11:46 AM Points: 34,581, Visits: 28,767
 Heh... for the same reason most British don't measure heat in British Thermal Units? --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #391150
 Posted Wednesday, August 15, 2007 10:49 PM
 SSC-Dedicated Group: General Forum Members Last Login: Yesterday @ 11:46 AM Points: 34,581, Visits: 28,767
 Incidentally, if you're using SQL 2005, you can jump straight to the final result fairly elegantly
Glad to see that someone finally figured out that there are 30 minutes between 12:30 and 13:00 and not just 29... Nicely done, Ryan.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

How to post code problems
How to post performance problems
Post #391153
 Posted Thursday, August 16, 2007 12:26 AM
 SSCommitted Group: General Forum Members Last Login: Tuesday, May 29, 2012 11:22 AM Points: 1,755, Visits: 4,652
 Thanks Jeff Ryan RandallSolutions are easy. Understanding the problem, now, that's the hard part.
Post #391163
 Posted Friday, August 17, 2007 12:28 AM
 Ten Centuries Group: General Forum Members Last Login: Sunday, September 16, 2012 3:26 AM Points: 1,038, Visits: 443
 Nice article. I like Ryan's elegant solution - unfortunately many of our clients are still sticking with SQL 2k (even though they could get by with Express) so I haven't played with CTEs.... The discussion about 59.59 or 60.00 in an hour is much the same as people who call midday 12am rather than 12pm, which day is 12am a part of, etc... (I know the answers, as you all would too, so no need to tell me )Only correction is that situation #6's description is wrong - the before/after is mixed up...
Post #391511
 Posted Friday, August 17, 2007 9:45 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, February 09, 2009 7:44 PM Points: 282, Visits: 37
 Hi,I didn't read the whole article, but had a couple on the more basic points.SELECTCOUNT(*)FROM[MY_EVENTS]WHERE---CASES 3, 4 AND 5(([STARTING_TIME] > 7/1/2007 12:00 PM AND [STARTING_TIME] < 7/1/2007 1:00 PM)OR ([ENDING_TIME] > 7/1/2007 12:00 PM AND [ENDING_TIME] < 7/1/2007 1:00 PM))OR---CASE 2([STARTING_TIME] < 7/1/2007 12:00 PM AND [ENDING_TIME] > 7/1/2007 1:00 PM)This could be written much more simply asSELECTCOUNT(*)FROM[MY_EVENTS]WHERE [STARTING_TIME]  <= 7/1/2007 1:00 PMAND [ENDING_TIME] >= 7/1/2007 12:00 PM                                                                                                        --This assumes that starting_time
Post #391692
 Posted Friday, August 17, 2007 1:32 PM
 Say Hey Kid Group: General Forum Members Last Login: Friday, September 21, 2012 1:52 PM Points: 696, Visits: 743
 what format would you like us to use? dd month year, without any punctuation at all? (example: today is 17 August 2007)the short date format, using two digits for month, day and year, is only ambiguous during the first 12 days of the month, when the order is not intuitively obvious (assuming a valid date). given that the average month is 365/12 or 30.42 days long, then ambiguity can only occur 39.5% of the time. Which is much greater precision than one gets in the form of specifications from an end user.
Post #391781
 Posted Friday, August 17, 2007 1:37 PM
 Say Hey Kid Group: General Forum Members Last Login: Friday, September 21, 2012 1:52 PM Points: 696, Visits: 743
 Robert - obviously you've never dealt with psychotherapists. Their hours range from 45 to 50 minutes.
Post #391783
 Posted Friday, August 17, 2007 3:14 PM
 Grasshopper Group: General Forum Members Last Login: Thursday, August 21, 2008 3:30 PM Points: 24, Visits: 6
 What about the situation where the Start date/times match? What about the situation where the Start and End date/times match? How does this affect the WHERE clause of the first query? Thanks
Post #391801

 Permissions