Solved

Using datetime to get data from certain hours of operation.

Posted on 2008-10-20
5
521 Views
Last Modified: 2012-06-27
I have a table with a datetime field, the table is huge, and has many records added per minute. I would like to query this table over the last month for certain hours of operation. Is there a way to write a query that will look at both a 'BETWEEN' certain dates, and 'BETWEEN' certain times on those dates?
0
Comment
Question by:Chuckbuchan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:caylt
ID: 22758934
Hello Chuck,

You'll need to use the Transact SQL DATEPART() built-in function to examine times specifically.
0
 
LVL 3

Accepted Solution

by:
caylt earned 300 total points
ID: 22758965
To illustrate:

select * from tblhistory where date between '2008-08-01' and '2008-08-31'
and datepart(hour, date) >= 9 and datepart(hour, date) <= 17

This returns all rows corresponding to August entries with times between 9am and 5pm.
0
 

Author Comment

by:Chuckbuchan
ID: 22759141
Using the first query below returns a count of zero.
The second returns a count of 5109.
Is the first query written poorly?
SELECT count(1)
FROM dbo.History 
WHERE calldatetime BETWEEN '2007-11-21' AND '2007-11-21'
AND DATEPART(HOUR, calldatetime) > 8 
AND DATEPART(HOUR, calldatetime) < 9
 
Select Count(1)
from touchstar.dbo.history
Where CallDateTime between '2007-11-21 08:00:00' 
and  '2007-11-21 09:00:00'

Open in new window

0
 
LVL 7

Assisted Solution

by:Cedric_D
Cedric_D earned 200 total points
ID: 22759444
the correct is:

SELECT count(1)
FROM dbo.History
WHERE calldatetime BETWEEN '2007-11-21 0:0:0' AND '2007-11-21 23:59:59'
AND DATEPART(HOUR, calldatetime) >= 8
AND DATEPART(HOUR, calldatetime) < 9
0
 

Author Closing Comment

by:Chuckbuchan
ID: 31507857
Thank you both for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question