| 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 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-08-29 : 09:55:05
|
| select * from mytable where processdatecolumn between sometime and othertimePeter LarssonHelsingborg, Sweden |
 |
|
|
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 |
 |
|
|
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 postedMadhivananFailing to plan is Planning to fail |
 |
|
|
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 dayeg.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 partswill 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 |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-08-30 : 12:43:04
|
| Try it. I think it should workMadhivananFailing to plan is Planning to fail |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2006-08-30 : 12:50:12
|
| you might also considerselect * from UrTbl where Date_Col between @StartDate and @EndDate + 1dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartDate and @EndDateand Date_Col - dateadd(day, 0, datediff(day, 0, Date_Col)) between @StartTime and @EndTimeThe 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. |
 |
|
|
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 datetimedeclare @end_date datetimedeclare @start_time datetimedeclare @end_time datetimeselect @start_date = '20060801', @end_date = dateadd(dd,1,'20060831'), @start_time = '08:30:00.000', @end_time = '17:00:00.000'select *from MyTable awhere 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 |
 |
|
|
|