Is there a way to accomplish something like this in SQL:
DECLARE @iter = 1
WHILE @iter<11
BEGIN
DECLARE @('newdate'+@iter) DATE = [some expression that generates a value]
SET @iter = @iter + 1
END
At the end I would have 10 variables:
@newdate1
@newdate2
@newdate3
@newdate4
@newdate5
@newdate6
@newdate7
@newdate8
@newdate9
@newdate10
Update:
Based on a comment, I think I should specify why I want to do this. I am working with Report Builder 3.0. I am going to make a report where the input will be a start date
and an end date
(in addition to one other parameter). This will generate data between the date range. However, the user also wants to check the same date range for all other years in the set 2013 -> current year.
The tricky part is this: the user can enter a date range in any year between 2013 and the current year and I need to return data for the input year and also data for the other years. For example, if the user enters in 1/1/2014 - 6/1/2014 then I need to return the same range but for the years 2013, 2015, and 2016.
Example input:
1/1/2016 - 6/1/2016
Report must generate data for these values:
1/1/2013 - 6/1/2013
1/1/2014 - 6/1/2014
1/1/2015 - 6/1/2015
1/1/2016 - 6/1/2016
If there is a better way to do this, I'm all ears.
To get start/end ranges only for last 4 years:
To get all dates in between:
To get all years from @START to 2013...
I use a UDF to create Dynamic Date Ranges.
For exanple
Returns
The UDF
Stripped Down version - NON UDF This can be injected into your SQL
John has a fancier solution than my simple example here, but this one doesn't need a separate UDF. In case you don't have permissions for those or something.
Whenever you're looking to generate lists of things that differ numerically (incrementally, etc.), think of using a Tally table (a table of numbers). Generating dates is a great application for a tally table:
Result:
Making calculations with a tally table in a query will often be much more efficient than loops, cursors, or dynamic SQL. In this case, compared to other answers presented, I'd say it's easier to program and maintain as well.
After seeing several of the other answers, I must say that I strongly encourage you to NOT create tons of numbered variables to hold these values. This would often be poor style in other languages where you might use an array or list or some other data structure, let alone SQL where sets and the means to manipulate and store them are fundamental to the language itself.
Perhaps I'm not seeing your particular use case, but even if you create these numbered variables using code, you'll then have to write more code to actually call these variables in any subsequent logic or calculations.
First, create a numbers table like this.
then create dates like this
EDIT: After new request, try this:
NOTE: It needs to be fixed for feb 29
Would something like this work for you? It uses dynamic SQL to build a query. I don't know about your date formats, but I used cast to sever the time portion from the standard getDate() function just in case.