SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Various Times From Various Dates
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

AskSQLTeam
Ask SQLTeam Question

USA
0 Posts

Posted - 08/29/2006 :  09:36:08  Show Profile  Visit AskSQLTeam's Homepage  Reply with Quote
Jeff writes "I am trying to write a query to retrieve information that was processed between two times of the day (2:00 pm to 6:00 pm) from a date range... I am perplexed; I am new to SQL and am trying not to become frustrated, but this seems to be beyond my skill level. Any guidence would be greatly appreciated!"

madhivanan
Premature Yak Congratulator

India
22765 Posts

Posted - 08/29/2006 :  09:54:46  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote

http://www.sql-server-performance.com/fk_datetime.asp


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30277 Posts

Posted - 08/29/2006 :  09:55:05  Show Profile  Visit SwePeso's Homepage  Reply with Quote
select * from mytable where processdatecolumn between sometime and othertime

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 08/29/2006 :  11:14:34  Show Profile  Reply with Quote
select * from UrTbl where 
	Convert(datetime, CONVERT(varchar(30), Date_Col, 101)) between @StartDate and @EndDate and
	Convert(datetime, CONVERT(varchar(30), Date_Col, 108)) between @StartTime and @EndTime


Srinika
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22765 Posts

Posted - 08/29/2006 :  12:08:21  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
quote:
Originally posted by Srinika

select * from UrTbl where 
	Convert(datetime, CONVERT(varchar(30), Date_Col, 101)) between @StartDate and @EndDate and
	Convert(datetime, CONVERT(varchar(30), Date_Col, 108)) between @StartTime and @EndTime


Srinika



You should always avoid using conversion on dates when comparing.
Read the Article I posted

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Srinika
Flowing Fount of Yak Knowledge

Sri Lanka
1378 Posts

Posted - 08/29/2006 :  12:47:56  Show Profile  Reply with Quote
Madhi,

How should the time portion is compared ?

I assumed: Within a given date range, and within a certain time range in each day

eg.
If the following is needed (Date From Aug 01 2006 to Aug 31 2006 and the time in between 8.30 AM & 5.00 PM)

how to compare the 2 time parts

will this work ?
select * from UrTbl where 
	dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartDate and @EndDate and
	Date_Col - dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartTime and @EndTime


Srinika
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22765 Posts

Posted - 08/30/2006 :  12:43:04  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Try it. I think it should work

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

nr
SQLTeam MVY

United Kingdom
12543 Posts

Posted - 08/30/2006 :  12:50:12  Show Profile  Visit nr's Homepage  Reply with Quote
you might also consider

select * from UrTbl where
Date_Col between @StartDate and @EndDate + 1
dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartDate and @EndDate
and Date_Col - dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartTime and @EndTime

The date comparison would have a chance of using an index

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 08/30/2006 :  13:59:40  Show Profile  Reply with Quote
You could do it like this:

declare @start_date datetime
declare @end_date datetime
declare @start_time datetime
declare @end_time datetime

select
	@start_date = '20060801',
	@end_date   = dateadd(dd,1,'20060831'),
	@start_time = '08:30:00.000',
	@end_time   = '17:00:00.000'

select *
from
	MyTable a
where 
	a.MyDate >= @start_date and
	a.MyDate <  @end_date   and
	-- Time Only Function: F_TIME_FROM_DATETIME
	-- http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=65358
	dbo.F_TIME_FROM_DATETIME( a.MyDate ) >= @start_time and
	dbo.F_TIME_FROM_DATETIME( a.MyDate ) <  @end_time


CODO ERGO SUM
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000