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
johnnyg123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>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.
0
dsackerContract ERP Admin/ConsultantCommented:
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.
0
johnnyg123Author Commented:
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
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

dsackerContract ERP Admin/ConsultantCommented:
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

0
johnnyg123Author Commented:
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
0
dsackerContract ERP Admin/ConsultantCommented:
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

0
Scott PletcherSenior DBACommented:
------------------------------------------------------------------------------------------------------------------------
-- input params: the value(s) you need to provide for each run
DECLARE @given_date date --or datetime
DECLARE @months_to_back_up smallint
DECLARE @months_to_list smallint
SET @given_date = '05/22/2015' --can be any day of month
SET @months_to_back_up = 4 --Example: if given date is in May, then start month will be Jan.
SET @months_to_list = 3 --Example: start month is Jan, then Jan to Mar listed; if thiis value were 2, Jan-Feb listed.

------------------------------------------------------------------------------------------------------------------------
-- main code [should work every time with no changes]
DECLARE @start_date datetime
-- calc below will always yield the first day of the month
SET @start_date = DATEADD(MONTH, DATEDIFF(MONTH, 0, @given_date) - @months_to_back_up, 0)

SELECT tn.col1, tn.col2 --,...
FROM table_name tn
WHERE
    tn.datetime_column >= @start_date AND
    tn.datetime_column < DATEADD(MONTH, @months_to_list, @start_date)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyg123Author Commented:
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!
0
dsackerContract ERP Admin/ConsultantCommented:
In that case, it sounds like you simply subtract 1 (if the start date is somewhat inclusive):
DECLARE @Date date,
        @StartDate date,
        @EndDate date

SET @Date = '2015-05-01'

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

SELECT  @StartDate AS StartDate,
        @EndDate   AS EndDate

Open in new window

0
johnnyg123Author Commented:
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
0
Scott PletcherSenior DBACommented:
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'
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.