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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query help - between dates

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 error


declare @dateto datetime
declare @datefrom datetime
select @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 datefrom
please 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))
Go to Top of Page

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)
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2008-01-06 : 12:53:24
so i did

declare @dateto datetime
declare @datefrom datetime
select @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
Go to Top of Page

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 )
Go to Top of Page
   

- Advertisement -