Solved

Using datetime to get data from certain hours of operation.

Posted on 2008-10-20
5
522 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

Technology Partners: 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

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…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
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…

717 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