SQL variable variables???
Posted on 2011-09-22
@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
where processingDate between @beginDate and @endDate
select a, b, c
where processingDate between @beginDate and @endDate)
Select sum(a), sum(b), sum(c)
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?