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
 date comparison

Author  Topic 

brucezepplin
Starting Member

15 Posts

Posted - 2011-07-06 : 04:55:28
Hi there, I have two tables, each having a date column where an event took place. I am looking for an sql statement where I pull out a date from the first table, and then bring all dates out from the second table that is plus or minus 10 days from the date in the first table.

so something like:

GROUP BY A.DATE, B.DATE
HAVING/WHERE B.DATE +/- A.DATE

apologies, but I have never really had to work with comparing daes before. thanks.

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-07-06 : 04:59:31
select abs(datediff(day,(getdate()+10),getdate())) ---10

select abs(datediff(day,getdate(),(getdate()+10))) ---10


--Ranjit
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-07-06 : 05:05:43
declare @Table table
(
id int
,startDate Datetime
,endDate Datetime
)

insert @Table
select 1,getdate(),getdate()+10 union all
select 2,getdate(),getdate()-10 union all
select 3,getdate(),getdate()+9

select *
from @Table
where abs(datediff(day,startDate,endDate))=10


--Ranjit
Go to Top of Page

brucezepplin
Starting Member

15 Posts

Posted - 2011-07-06 : 05:22:19
thanks ranjit. can i use A.EVENT,B.EVENT as parameters in the DATEDIFF function?

ie abs(datediff(day,A.EVENT,B.EVENT))=10 ?
Go to Top of Page

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-07-06 : 05:27:42
Yes You can Use

--Ranjit
Go to Top of Page
   

- Advertisement -