Link to home
Start Free TrialLog in
Avatar of johnnyg123
johnnyg123Flag for United States of America

asked on

Sql query for data range

Trying to write a query that will return  a 3 month range that will start 2 months back from a given date
(SQL server 2008)

For example if given date is '05/01/2015'


I would want

Startdate               EndDate
01/01/2015           03/31/2015
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

>that will start 2 months back from a given date
>For example if given date is '05/01/2015'
Wouldn't this make the start date 3/1/2015?  Your stated results shows 1/1/2015.
Try this:
DECLARE @Date datetime,
        @StartDate datetime,
        @EndDate datetime

SET @Date = '2015-04-01'

SET @StartDate = CONVERT(DATE, DATEADD(month, -2, @Date))  -- As of midnight
SET @EndDate   = CONVERT(DATE, DATEADD(month, 3, @StartDate))
SET @EndDate   = @EndDate - .00000005   --   Makes it as of 23:59:59.997

SELECT *
FROM   YourTable
WHERE  YourDate BETWEEN @StartDate AND @EndDate

Open in new window

There are certainly other ways to do this by eliminating some of the dates and consolidating the math, but I'm assuming you may have need for the @StartDate and @EndDate, so am giving this as is.
Avatar of johnnyg123

ASKER

oops   meant that the end date of the range should be 2 month s back

What I'm trying to do is get a 3 month range that has end date that goes back 1 month from a given date

basically our marketing department  has a promotion that starts the first of each month (They like to work in the future)

They want me to give a list of coupons that were redeemed in a 3 month period prior
They do not want to include the month immediately prior to the given date in that range (not exactly sure why but)


so if the given date is 05/01/2015  they would the range to be  01/01/2015 to 03/31/2015

likewise if the given date was 06/01/2015  the range would be 02/01/2015 to 04/30/2015


Hope that makes sense
This should work:
DECLARE @Date datetime,
        @StartDate datetime,
        @EndDate datetime

SET @Date = '2015-05-01'

SET @EndDate   = CONVERT(DATE, DATEADD(month, -2, @Date))
SET @StartDate = CONVERT(DATE, DATEADD(month, -3, @EndDate))
SET @EndDate   = @EndDate - .00000005   --   Makes it as of 23:59:59.997

SELECT *
FROM   YourTable
WHERE  YourDate BETWEEN @StartDate AND @EndDate

Open in new window

I do not need the time so I change it to the following

If I run

DECLARE @Date date,
        @StartDate date,
        @EndDate date

SET @Date = '2015-05-01'

SET @EndDate   = CONVERT(DATE, DATEADD(month, -2, @Date))
SET @StartDate = CONVERT(DATE, DATEADD(month, -3, @EndDate))


select   @StartDate as startdate,
        @EndDate  as enddate

I get

startdate      enddate
2014-12-01      2015-03-01

Instead of 01/01/2015 to 03/31/2015

startdate      enddate
2015-01-01      2015-03-31
I answered with datetime on the side of caution. However, subtracting that .000005 to achieve 23:59:59.997 will need to be replaced by subtracting a day, so that you don't wind up with a range of 3 months + 1 day.

This will solve that:
DECLARE @Date date,
        @StartDate date,
        @EndDate date

SET @Date = '2015-05-01'

SET @EndDate   = DATEADD(month, -2, @Date)
SET @StartDate = DATEADD(month, -3, @EndDate)
SET @EndDate   = DATEADD(day, -1, @EndDate)

SELECT  @StartDate AS StartDate,
        @EndDate   AS EndDate

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry I think I'm not explaining my self to well


Then end result should be

startdate      enddate
 2015-01-01      2015-03-31

I am getting

startdate      enddate
 2014-12-01      2015-03-01

executing the code you provided


I should mention that the given date will always be the first of a month

since in the example  start date is  May 1

need to skip the month of april

so march is the end month and  thus 03/31/2015 is the end date

then need to include 2 months prior to march to get a total of 3  which would be jan and feb thus the start date of jan 01 2015

Hope that helps!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the Posts!

I used a combined approach so I will split the points

here is what I went with

DECLARE @given_date date
 DECLARE @months_to_back_up smallint
 DECLARE @months_to_list smallint
 SET @given_date = '05/31/2015'
 SET @months_to_back_up = 3
 SET @months_to_list = 3

 DECLARE @start_date date
 DECLARE @End_date date
 SET @start_date = DATEADD(MONTH, DATEDIFF(MONTH , 0, @given_date ) - (@months_to_back_up + 1) , 0)
 SET @End_date = Dateadd(day,-1,DATEADD(MONTH, @months_to_list, @start_date))

 select @start_date startdate, @end_date enddate
You should always check date/datetime/datetime2 values using >= and <, not <=.  That insures both accuracy and performance, even if the data type of the table column changes, such as from datetime to datetime2 or from date to datetime.

That is, to check for the month of Jan, do this:
>= '20150101' AND < '20150201'
and not this:
>= '20150101' AND <= '20150131'