Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Various Times From Various Dates

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2006-08-29 : 09:36:08
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

22864 Posts

Posted - 2006-08-29 : 09:54:46

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

30421 Posts

Posted - 2006-08-29 : 09:55:05
select * from mytable where processdatecolumn between sometime and othertime

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-08-29 : 11:14:34
[code]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[/code]

Srinika
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-08-29 : 12:08:21
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
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-08-29 : 12:47:56
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

22864 Posts

Posted - 2006-08-30 : 12:43:04
Try it. I think it should work

Madhivanan

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

nr
SQLTeam MVY

12543 Posts

Posted - 2006-08-30 : 12:50:12
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)

7020 Posts

Posted - 2006-08-30 : 13:59:40
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
   

- Advertisement -