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.
| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-01-06 : 07:49:23
|
| here's my sql that is return an errordeclare @dateto datetimedeclare @datefrom datetimeselect @dateto='2007-12-30'select @datefrom='2007-12-15'select * from exem where (datetime between(@dateto and @dateto+21) or datetime between(@datefrom and @datefrom+21))what i want is any record in exem where the datetime is 21 days from the dateto or datefromplease advise? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-06 : 09:40:02
|
| select * from exem where (datetime between @dateto and DATEADD(d,21,@dateto)) or (datetime between @datefrom and DATEADD(d,21,@datefrom)) |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-01-06 : 10:56:15
|
| select * from exem where (datetime between @dateto and @dateto+21)or (datetime between @datefrom and @datefrom+21) |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2008-01-06 : 12:53:24
|
| so i diddeclare @dateto datetimedeclare @datefrom datetimeselect @dateto='2007-12-31'select @datefrom='2007-12-30'select * from exem where (datetime between @dateto and DATEADD(d,21,@dateto))or (datetime between @datefrom and DATEADD(i have a bunch of records with dateime as '2007-12-15' why wouldn't they show - it's within 21 days |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-06 : 23:15:28
|
| '2007-12-15' come before your datefrom and dateto. You had asked for getting records b/w datefrom/dateto and datefrom/dateto + 21. For getting records with dates within 21 days backward just replace 21 with -21.i.e select * from exem where (datetime between DATEADD(d,-21,@dateto) and @dateto ) or (datetime between DATEADD(d,-21,@datefrom) and @datefrom ) |
 |
|
|
|
|
|