| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-26 : 13:50:18
|
| I have the following query select * from stats where (statsdatetime>='2007-03-20 8:00:00' and statsdatetime<='2007-03-26 18:00:00')this is returning records with a date of 2007-03-24 18:26:06.000why? how can I get only records between the horus of 8:00 and 18:00?similarly i also need an oppostite query with all between the horus of 18:00 and 7:59 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-26 : 13:56:48
|
| select * from statswhere statsdatetime >= '20070320' and statsdatetime < '20070327'and datepart(hour, statsdatetime) >= 8 and datepart(hour, statsdatetime) < 18select * from statswhere statsdatetime >= '20070320' and statsdatetime < '20070327'and datepart(hour, statsdatetime) < 8 and datepart(hour, statsdatetime) >= 18Peter LarssonHelsingborg, Sweden |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-26 : 14:00:28
|
| Your query looks for rows that are within a range starting at midnight on 3/20 and ending at 18:00 on 3/26. If you want to limit time of day, you have to specifically look at time of day, like thisselect * from stats where (statsdatetime>='2007-03-20 8:00:00' and statsdatetime<='2007-03-26 18:00:00') and datepart(hh, statsdatetime) between 8 and 17or for the other hoursselect * from stats where (statsdatetime>='2007-03-20 8:00:00' and statsdatetime<='2007-03-26 18:00:00') and datepart(hh, statsdatetime) not between 8 and 17 |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-26 : 14:08:07
|
| thanks... didn't spot thatand datepart(hh, statsdatetime) between 8 and 17how do I say between 8:00 and 17:59and does between include the 17:59 minute? |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-26 : 15:07:05
|
| thanks for your help - i got that workingnow i'm trying to add onto that query and (DATENAME(dw , statsdatetime)<>'Sunday' Or DATENAME(dw , statsdatetime) <>'Saturday')this is returning results on saturday or sunday - what am i doing wrong? |
 |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-03-26 : 15:21:58
|
| Your logic is wrong, that should be and (DATENAME(dw , statsdatetime)<>'Sunday' and DATENAME(dw , statsdatetime) <>'Saturday')Or, more simply you could useand DATEPART(dw , statsdatetime) between 2 and 6 |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2007-03-26 : 15:28:20
|
| thanks for spotting that...:) |
 |
|
|
|