SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Working with Time Frames in T-SQL


Working with Time Frames in T-SQL

Author
Message
jlion
jlion
SSC Rookie
SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)SSC Rookie (36 reputation)

Group: General Forum Members
Points: 36 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
Robert Davis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11931 Visits: 1666

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
SQL Server Best Practices: SQL Server Best Practices
Twitter: @SQLSoldier
My book: Pro SQL Server 2008 Mirroring
Microsoft Certified Master: SQL Server, Data Platform MVP
Database Engineer at BlueMountain Capital Management
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)

Group: General Forum Members
Points: 382577 Visits: 43026
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)SSC Guru (382K reputation)

Group: General Forum Members
Points: 382577 Visits: 43026
quoteIncidentally, 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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
RyanRandall
RyanRandall
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

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

Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Ian Yates
Ian Yates
SSCrazy Eights
SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)SSCrazy Eights (8.1K reputation)

Group: General Forum Members
Points: 8068 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
Dave F-425609
SSC-Addicted
SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)SSC-Addicted (458 reputation)

Group: General Forum Members
Points: 458 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
steve smith-401573
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4443 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
steve smith-401573
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4443 Visits: 744
Robert - obviously you've never dealt with psychotherapists. Their hours range from 45 to 50 minutes.
Me Da Man
Me Da Man
SSC Journeyman
SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)SSC Journeyman (88 reputation)

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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search