T-SQL specific date range without hardcoding

Hi all,

I am trying to get data returned for a specific date range. So for example from the period 2013-07-01 to 2014-08-31 however the year will change year by year. Usually I would just do this
WHERE date BETWEEN '2013-07-01' AND '2014-08-01'

Open in new window

but that doesn't meet my needs as I also need it to be able to automatically pick up '2014-07-01' to '2015-08-31' next year.

I have seen a few examples using
DATEADD(mm,-7,GETDATE() 

Open in new window

for example which will take me back 7 months however I have specific months I am looking at so I'm struggling to see how getdate can help me out here.

Any ideas would be much appreciated.
ric134Asked:
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.

PortletPaulEE Topic AdvisorCommented:
You have not specified what you want exactly, except that getdate() - 7 months isn't correct.

Is this a fiscal year requirement?
except: if a fiscal year starts 1st July, the end of the fiscal year occurs on 30th June (not 31st August)

---
+ although not requested, you should avoid using BETWEEN for date ranges.
Vitor MontalvãoMSSQL Senior EngineerCommented:
You should give us the complete view.
Is this a stored procedure, a single SELECT or a script with more statements?
ric134Author Commented:
Hi Paul,

Yes I guess you could say it is a fiscal year requirement or more specifically an academic year requirement (UK)  so it runs from the  1st August to the 31st July which I'll edit above as I can see I haven't got that right.

What I am after is any records that have an attendance date value between 2013-08-01 and 2014-07-31 then I intended to have a case statement to show this as 2013/14. Anything after -  so for example 2014-08-01 - needs to be shown as 2014/15. As the years go by however I'm only interested in the last academic year and the current academic year so I need some way of doing this without hard coding.

Sorry if If I'm still not being clear, I hope this makes more sense.

Update: Hi vitor, this is just a simple query I'm working on at the minute but I've hit a block with this one.
PortletPaulEE Topic AdvisorCommented:
try this
declare @thisyrstart as datetime
declare @prevyrstart as datetime
declare @nextyrstart as datetime

set @thisyrstart = dateadd(year,
   case when month(getdate()) > 7 then datediff(year,0,getdate())+1 else datediff(year,0,getdate()) end
                           , '19000801')
set @prevyrstart = dateadd(year,-1,@thisyrstart)
set @nextyrstart = dateadd(year, 1,@thisyrstart)

select
    @prevyrstart
  , @thisyrstart
  , @nextyrstart
  , attendance_date
  , case when attendance_date >= @prevyrstart and attendance_date < @thisyrstart then 'prev'
         when attendance_date >= @thisyrstart and attendance_date < @nextyrstart then 'this'
         else 'wrong'
    end as acyear
  from table1
  where attendance_date >= @prevyrstart and attendance_date < @nextyrstart
;

Open in new window


details:
    CREATE TABLE Table1
        ([attendance_date] datetime)
    ;
        
    INSERT INTO Table1
        ([attendance_date])
    VALUES
        ('2012-08-01 00:00:00'),
        ('2012-08-24 00:00:00'),
        ('2012-09-16 00:00:00'),
        ('2012-10-09 00:00:00'),
        ('2012-11-01 00:00:00'),
        ('2012-11-24 00:00:00'),
        ('2012-12-17 00:00:00'),
        ('2013-01-09 00:00:00'),
        ('2013-02-01 00:00:00'),
        ('2013-02-24 00:00:00'),
        ('2013-03-19 00:00:00'),
        ('2013-04-11 00:00:00'),
        ('2013-05-04 00:00:00'),
        ('2013-05-27 00:00:00'),
        ('2013-06-19 00:00:00'),
        ('2013-07-12 00:00:00'),
        ('2013-08-04 00:00:00'),
        ('2013-08-27 00:00:00'),
        ('2013-09-19 00:00:00'),
        ('2013-10-12 00:00:00'),
        ('2013-11-04 00:00:00'),
        ('2013-11-27 00:00:00'),
        ('2013-12-20 00:00:00'),
        ('2014-01-12 00:00:00'),
        ('2014-02-04 00:00:00'),
        ('2014-02-27 00:00:00'),
        ('2014-03-22 00:00:00'),
        ('2014-04-14 00:00:00'),
        ('2014-05-07 00:00:00'),
        ('2014-05-30 00:00:00'),
        ('2014-06-22 00:00:00'),
        ('2014-07-15 00:00:00'),
        ('2014-08-07 00:00:00'),
        ('2014-08-30 00:00:00'),
        ('2014-09-22 00:00:00'),
        ('2014-10-15 00:00:00'),
        ('2014-11-07 00:00:00'),
        ('2014-11-30 00:00:00'),
        ('2014-12-23 00:00:00'),
        ('2015-01-15 00:00:00'),
        ('2015-02-07 00:00:00'),
        ('2015-03-02 00:00:00'),
        ('2015-03-25 00:00:00'),
        ('2015-04-17 00:00:00'),
        ('2015-05-10 00:00:00'),
        ('2015-06-02 00:00:00'),
        ('2015-06-25 00:00:00'),
        ('2015-07-18 00:00:00'),
        ('2015-08-10 00:00:00'),
        ('2015-09-02 00:00:00'),
        ('2015-09-25 00:00:00'),
        ('2015-10-18 00:00:00'),
        ('2015-11-10 00:00:00'),
        ('2015-12-03 00:00:00'),
        ('2015-12-26 00:00:00'),
        ('2016-01-18 00:00:00'),
        ('2016-02-10 00:00:00'),
        ('2016-03-04 00:00:00'),
        ('2016-03-27 00:00:00'),
        ('2016-04-19 00:00:00'),
        ('2016-05-12 00:00:00'),
        ('2016-06-04 00:00:00'),
        ('2016-06-27 00:00:00'),
        ('2016-07-20 00:00:00'),
        ('2016-08-12 00:00:00'),
        ('2016-09-04 00:00:00'),
        ('2016-09-27 00:00:00'),
        ('2016-10-20 00:00:00'),
        ('2016-11-12 00:00:00'),
        ('2016-12-05 00:00:00'),
        ('2016-12-28 00:00:00'),
        ('2017-01-20 00:00:00'),
        ('2017-02-12 00:00:00'),
        ('2017-03-07 00:00:00'),
        ('2017-03-30 00:00:00'),
        ('2017-04-22 00:00:00'),
        ('2017-05-15 00:00:00'),
        ('2017-06-07 00:00:00'),
        ('2017-06-30 00:00:00'),
        ('2017-07-23 00:00:00'),
        ('2017-08-15 00:00:00'),
        ('2017-09-07 00:00:00'),
        ('2017-09-30 00:00:00'),
        ('2017-10-23 00:00:00'),
        ('2017-11-15 00:00:00'),
        ('2017-12-08 00:00:00'),
        ('2017-12-31 00:00:00'),
        ('2018-01-23 00:00:00'),
        ('2018-02-15 00:00:00'),
        ('2018-03-10 00:00:00'),
        ('2018-04-02 00:00:00'),
        ('2018-04-25 00:00:00'),
        ('2018-05-18 00:00:00'),
        ('2018-06-10 00:00:00'),
        ('2018-07-03 00:00:00'),
        ('2018-07-26 00:00:00'),
        ('2018-08-18 00:00:00'),
        ('2018-09-10 00:00:00'),
        ('2018-10-03 00:00:00'),
        ('2018-10-26 00:00:00'),
        ('2018-11-18 00:00:00'),
        ('2018-12-11 00:00:00'),
        ('2019-01-03 00:00:00'),
        ('2019-01-26 00:00:00'),
        ('2019-02-18 00:00:00'),
        ('2019-03-13 00:00:00'),
        ('2019-04-05 00:00:00'),
        ('2019-04-28 00:00:00'),
        ('2019-05-21 00:00:00'),
        ('2019-06-13 00:00:00'),
        ('2019-07-06 00:00:00'),
        ('2019-07-29 00:00:00'),
        ('2019-08-21 00:00:00'),
        ('2019-09-13 00:00:00'),
        ('2019-10-06 00:00:00'),
        ('2019-10-29 00:00:00'),
        ('2019-11-21 00:00:00'),
        ('2019-12-14 00:00:00'),
        ('2020-01-06 00:00:00'),
        ('2020-01-29 00:00:00'),
        ('2020-02-21 00:00:00'),
        ('2020-03-15 00:00:00'),
        ('2020-04-07 00:00:00'),
        ('2020-04-30 00:00:00'),
        ('2020-05-23 00:00:00'),
        ('2020-06-15 00:00:00'),
        ('2020-07-08 00:00:00'),
        ('2020-07-31 00:00:00'),
        ('2020-08-23 00:00:00'),
        ('2020-09-15 00:00:00'),
        ('2020-10-08 00:00:00'),
        ('2020-10-31 00:00:00'),
        ('2020-11-23 00:00:00'),
        ('2020-12-16 00:00:00'),
        ('2021-01-08 00:00:00'),
        ('2021-01-31 00:00:00'),
        ('2021-02-23 00:00:00'),
        ('2021-03-18 00:00:00'),
        ('2021-04-10 00:00:00'),
        ('2021-05-03 00:00:00'),
        ('2021-05-26 00:00:00'),
        ('2021-06-18 00:00:00'),
        ('2021-07-11 00:00:00'),
        ('2021-08-03 00:00:00'),
        ('2021-08-26 00:00:00'),
        ('2021-09-18 00:00:00'),
        ('2021-10-11 00:00:00'),
        ('2021-11-03 00:00:00'),
        ('2021-11-26 00:00:00'),
        ('2021-12-19 00:00:00'),
        ('2022-01-11 00:00:00'),
        ('2022-02-03 00:00:00'),
        ('2022-02-26 00:00:00'),
        ('2022-03-21 00:00:00'),
        ('2022-04-13 00:00:00'),
        ('2022-05-06 00:00:00'),
        ('2022-05-29 00:00:00'),
        ('2022-06-21 00:00:00'),
        ('2022-07-14 00:00:00'),
        ('2022-08-06 00:00:00'),
        ('2022-08-29 00:00:00'),
        ('2022-09-21 00:00:00'),
        ('2022-10-14 00:00:00'),
        ('2022-11-06 00:00:00'),
        ('2022-11-29 00:00:00'),
        ('2022-12-22 00:00:00'),
        ('2023-01-14 00:00:00'),
        ('2023-02-06 00:00:00'),
        ('2023-03-01 00:00:00'),
        ('2023-03-24 00:00:00'),
        ('2023-04-16 00:00:00'),
        ('2023-05-09 00:00:00'),
        ('2023-06-01 00:00:00'),
        ('2023-06-24 00:00:00'),
        ('2023-07-17 00:00:00'),
        ('2023-08-09 00:00:00'),
        ('2023-09-01 00:00:00'),
        ('2023-09-24 00:00:00'),
        ('2023-10-17 00:00:00'),
        ('2023-11-09 00:00:00'),
        ('2023-12-02 00:00:00'),
        ('2023-12-25 00:00:00'),
        ('2024-01-17 00:00:00'),
        ('2024-02-09 00:00:00'),
        ('2024-03-03 00:00:00'),
        ('2024-03-26 00:00:00'),
        ('2024-04-18 00:00:00'),
        ('2024-05-11 00:00:00'),
        ('2024-06-03 00:00:00'),
        ('2024-06-26 00:00:00'),
        ('2024-07-19 00:00:00'),
        ('2024-08-11 00:00:00'),
        ('2024-09-03 00:00:00'),
        ('2024-09-26 00:00:00'),
        ('2024-10-19 00:00:00'),
        ('2024-11-11 00:00:00'),
        ('2024-12-04 00:00:00'),
        ('2024-12-27 00:00:00'),
        ('2025-01-19 00:00:00'),
        ('2025-02-11 00:00:00'),
        ('2025-03-06 00:00:00'),
        ('2025-03-29 00:00:00'),
        ('2025-04-21 00:00:00'),
        ('2025-05-14 00:00:00'),
        ('2025-06-06 00:00:00'),
        ('2025-06-29 00:00:00'),
        ('2025-07-22 00:00:00'),
        ('2025-08-14 00:00:00'),
        ('2025-09-06 00:00:00'),
        ('2025-09-29 00:00:00'),
        ('2025-10-22 00:00:00'),
        ('2025-11-14 00:00:00'),
        ('2025-12-07 00:00:00'),
        ('2025-12-30 00:00:00'),
        ('2026-01-22 00:00:00'),
        ('2026-02-14 00:00:00'),
        ('2026-03-09 00:00:00'),
        ('2026-04-01 00:00:00'),
        ('2026-04-24 00:00:00'),
        ('2026-05-17 00:00:00'),
        ('2026-06-09 00:00:00'),
        ('2026-07-02 00:00:00'),
        ('2026-07-25 00:00:00'),
        ('2026-08-17 00:00:00'),
        ('2026-09-09 00:00:00'),
        ('2026-10-02 00:00:00'),
        ('2026-10-25 00:00:00'),
        ('2026-11-17 00:00:00'),
        ('2026-12-10 00:00:00'),
        ('2027-01-02 00:00:00'),
        ('2027-01-25 00:00:00'),
        ('2027-02-17 00:00:00'),
        ('2027-03-12 00:00:00'),
        ('2027-04-04 00:00:00'),
        ('2027-04-27 00:00:00'),
        ('2027-05-20 00:00:00'),
        ('2027-06-12 00:00:00'),
        ('2027-07-05 00:00:00'),
        ('2027-07-28 00:00:00'),
        ('2027-08-20 00:00:00'),
        ('2027-09-12 00:00:00'),
        ('2027-10-05 00:00:00'),
        ('2027-10-28 00:00:00'),
        ('2027-11-20 00:00:00'),
        ('2027-12-13 00:00:00'),
        ('2028-01-05 00:00:00'),
        ('2028-01-28 00:00:00'),
        ('2028-02-20 00:00:00')
    ;
    
**Query 1**:

    declare @thisyrstart as datetime
    declare @prevyrstart as datetime
    declare @nextyrstart as datetime
    
    set @thisyrstart = dateadd(year,
       case when month(getdate()) > 7 then datediff(year,0,getdate())+1 else datediff(year,0,getdate()) end
                               , '19000801')
    set @prevyrstart = dateadd(year,-1,@thisyrstart)
    set @nextyrstart = dateadd(year, 1,@thisyrstart)
    
    select
        @prevyrstart
      , @thisyrstart
      , @nextyrstart
      , attendance_date
      , case when attendance_date >= @prevyrstart and attendance_date < @thisyrstart then 'prev'
             when attendance_date >= @thisyrstart and attendance_date < @nextyrstart then 'this'
             else 'wrong'
        end as acyear
      from table1
      where attendance_date >= @prevyrstart and attendance_date < @nextyrstart
    

**[Results][2]**:
    |                          |                          |                          |             attendance_date | acyear |
    |--------------------------|--------------------------|--------------------------|-----------------------------|--------|
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |    August, 07 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |    August, 30 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 | September, 22 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |   October, 15 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  November, 07 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  November, 30 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  December, 23 2014 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |   January, 15 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  February, 07 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     March, 02 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     March, 25 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     April, 17 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |       May, 10 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      June, 02 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      June, 25 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      July, 18 2015 00:00:00 |   prev |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |    August, 10 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 | September, 02 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 | September, 25 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |   October, 18 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  November, 10 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  December, 03 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  December, 26 2015 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |   January, 18 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |  February, 10 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     March, 04 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     March, 27 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |     April, 19 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |       May, 12 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      June, 04 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      June, 27 2016 00:00:00 |   this |
    | August, 01 2014 00:00:00 | August, 01 2015 00:00:00 | August, 01 2016 00:00:00 |      July, 20 2016 00:00:00 |   this |

  [1]: http://sqlfiddle.com/#!6/0cc9f4/6
  [2]: http://sqlfiddle.com/#!6/0cc9f4/6/0

Open in new window


{+edit}
I may be 1 year away from what you are looking for, but you can adjust that by altering line 6 of first code block

e.g
   case when month(getdate()) > 7 then datediff(year,0,getdate()) else datediff(year,0,getdate())-1 end

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
Scott PletcherSenior DBACommented:
DECLARE @year_adjustment smallint

SET @year_adjustment = CASE WHEN MONTH(GETDATE()) >= 8 THEN 0 ELSE -1 END


SELECT ...
...
WHERE
    attendance_date >= CAST(YEAR(GETDATE()) + @year_adjustment AS char(4)) + '0801' AND
    attendance_date < CAST(YEAR(GETDATE()) + @year_adjustment + 1 AS char(4)) + '0801'
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 2008

From novice to tech pro — start learning today.