tags:

views:

34

answers:

2

Possible Duplicate:
90 days range using SQL server

I am trying to get the counts 90 days prior to the operational date and the counts 90 days after the operational date. For example, my operational date is 4/1/2004. So,90 days prior to 4/1/2004 is (1/2/2004 to 3/31/2004) and 90 days after (including 4/1/2004) is 6/29/2004.

I used the following scripts and mannually calculate the days, which is not efficient...

select
site,
count(*) as prior_counts
from mytable
where mydate >='1/2/2004'
and mydate <'4/1/2004'
group by site


select
site,
count(*) as after_counts
from mytable
where mydate >='4/1/2004'
and mydate <'6/30/2004'
group by site
A: 

You should look at the DATEDIFF function or equivalent if you're not using SQL Server.

DATEDIFF on MSDN

Jason
A: 

If you are passing the date parameter in from an application, consider modifying the application to do the date range calculation for you. By passing in two parameters, you are taking the burden of the calculation off SQL, which will improve performance.

DJ Quimby