## Working with Time Frames in T-SQL

Author
Message
jlion
Forum Newbie

Group: General Forum Members
Points: 2 Visits: 53
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.

Robert Davis
SSCommitted

Group: General Forum Members
Points: 1702 Visits: 1623

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 datetime
Declare
@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)
End

Select *
From
@Minutes

My blog: SQL Soldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master, SQL Server MVP
Database Engineer at BlueMountain Capital Management
Jeff Moden
SSC-Forever

Group: General Forum Members
Points: 45119 Visits: 39918
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should.

How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
SSC-Forever

Group: General Forum Members
Points: 45119 Visits: 39918
 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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should.

How to post code problems
How to post performance problems
Forum FAQs
RyanRandall
SSCommitted

Group: General Forum Members
Points: 1761 Visits: 4652
Thanks Jeff

Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Ian Yates
Ten Centuries

Group: General Forum Members
Points: 1056 Visits: 445
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...

Dave F-425609
SSC Veteran

Group: General Forum Members
Points: 282 Visits: 37

Hi,

I didn't read the whole article, but had a couple on the more basic points.

SELECT
COUNT(*)
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 as

SELECT
COUNT(*)
FROM
[MY_EVENTS]
WHERE [STARTING_TIME] <= 7/1/2007 1:00 PM
AND [ENDING_TIME] >= 7/1/2007 12:00 PM --This assumes that starting_time<ending_time

Also to get the time period utilised for each row you could do something like

(((7/1/2007 1:00PM)-(START_TIME)-(CASE END_TIME<(7/1/2007 1:00PM) THEN ((7/1/2007 1:00PM)-(END_TIME)) ELSE 0))%60)*24*60

to get the number of minutes

Not criticising, it's just what I thought of what I began, and couldn't continue until I wrote it down

steve smith-401573
Say Hey Kid

Group: General Forum Members
Points: 695 Visits: 744
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.
steve smith-401573
Say Hey Kid

Group: General Forum Members
Points: 695 Visits: 744
Robert - obviously you've never dealt with psychotherapists. Their hours range from 45 to 50 minutes.
Me Da Man
Grasshopper

Group: General Forum Members
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