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
 Matching up dates

Author  Topic 

brucezepplin
Starting Member

15 Posts

Posted - 2011-07-26 : 05:50:25
Hi everyone. I am looking to compare dates in SQL - do a bit of arithmetic on them. What I have is:

Two events - Buying a car, and Servicing the car. I am interested in the people who have had to take their car in for a service within one month of buying their car. So the code I have is:


SELECT * FROM

(SELECT DISTINCT Customer, Event_type, Event_date
FROM Database
WHERE EVENT_type = "Purchase") A

INNER JOIN

(SELECT DISTINCT Customer, Event_type, Event_date
FROM Database
WHERE EVENT_type = "Service") B

-- bit of code I am struggling with below
WHERE B.Event_date - A.Event_date BETWEEN '1' AND '30'


So my logic is that if the service date - the purchase date is between 1 and 30, then they have taken their car in for a service within the first 30 days of having it.

Any help with that last line of code?

Thanks in advance!

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-07-26 : 06:07:02
Soemthing like this?

WHERE B.Event_date <= DATEADD(dd,30,A.Event_date)
Go to Top of Page

brucezepplin
Starting Member

15 Posts

Posted - 2011-07-26 : 06:14:30
But would that search for a service date any time up to 30 days after the purchase, or just on the 30th day after the purchase?
Go to Top of Page

brucezepplin
Starting Member

15 Posts

Posted - 2011-07-26 : 06:17:18
just saw the less than or equal to.....my bad!
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

3608 Posts

Posted - 2011-07-26 : 07:55:48
Go to Top of Page
   

- Advertisement -