SQL variable variables???

Posted on 2011-09-22
Last Modified: 2012-06-27
@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?
Question by:simplyfemales
  • 2
  • 2
LVL 59

Accepted Solution

Kevin Cross earned 500 total points
Comment Utility

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!

Author Comment

Comment Utility
so I need to include ProcessingDate as a part of the select statements correct?  
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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.

Author Closing Comment

Comment Utility
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.

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now