?
Solved

Using datetime to get data from certain hours of operation.

Posted on 2008-10-20
5
Medium Priority
?
523 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 1200 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 800 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

Independent Software Vendors: 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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

800 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