SQL variable variables???

@beginDate date = convert(varchar,DATEADD(MONTH,DATEDIFF(MONTH,0,getdate()),0),101),
@endDate Date = convert(varchar,DATEADD(MONTH,DATEDIFF(MONTH,-1,getdate()),-1),101);

with aggregate as

(select a, b, c
from Table
where processingDate between @beginDate and @endDate
union all
select a, b, c
from Table2
where processingDate between @beginDate and @endDate)

Select  sum(a), sum(b), sum(c)
from aggregate

What I would like to know is this.  How can I work with the variables properly so that I can do this as a monthly thing?  I want 6 months of data, meaning.... last months, then the month before that, then the month before that, etc.

What is the best way to approach this?  If I declare the variable as something fixed, it doesn't work.  I need to make the variable... variable... but how?
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Kevin CrossConnect With a Mentor Chief Technology OfficerCommented:

Is this is a rolling six months, i.e., when you run the report it should always give you the last full six months. For example running this today should give you August back to March. I would just get rid of the variables and put that into code; however, if you want the variables, note that you do not need to convert to US English format only to store as a Date which could care less about format. i.e., SQL does not store dates in a given format. That is strictly a display issue.

So these are the values you need:

You would then use then in your query as:
WHERE processingDate >= @beginDate AND  processingDate < @endDate

The @endDate is the first day of the current month at midnight, so the < is intentional. This helps with data that has timestamps as it will include all timestamps from the previous day up to but not including midnight. I guess if this is a date column, the between is fine, just make it:
WHERE processingDate BETWEEN @beginDate AND DATEADD(DAY, -1, @endDate)

Then I would GROUP BY DATEDIFF(MONTH, 0, processingDate), so that you get your sums for each of the six months in one report that you can convert back to a DATE for display (i.e., first day of each month). Another option is to show as month's ago, by doing your DATEDIFF(MONTH, processingDate, GETDATE()). Then you can consistently report on 1, 2, 3, 4, 5, or 6 months ago without worrying about actual date name.

Hope that helps!
simplyfemalesAuthor Commented:
so I need to include ProcessingDate as a part of the select statements correct?  
Kevin CrossChief Technology OfficerCommented:
Correct, except you want to use one of the variations I showed using DATEDIFF() or else you will get sums by day instead of by month.
simplyfemalesAuthor Commented:
This is excellent.  Exactly what I need.  It reminded me of how basic the queries need to be sometimes.... just go back to what you know rather than trying to overthink it.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.