Solved

Using datetime to get data from certain hours of operation.

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

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.…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now