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
Solved

Using datetime to get data from certain hours of operation.

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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