Question

Is it possible to create parameters in SQL that are dynamic? That is to say, calculation based on the values assigned to another parameter. For example I am currently passing values into my stored proc like this:

       IF (@SP_Season = 'F13')
        Begin
        Set @PriorSeason_2012 = 'F12'
        Set @PriorSeason_2011 = 'F11'
        END

   IF (@SP_Season = 'F14')
    Begin
    Set @PriorSeason_1 = 'F13'
    Set @PriorSeason_2012 = 'F12'
    END

   IF (@SP_Season = 'F15')
    Begin
    Set @PriorSeason_1 = 'F14'
    Set @PriorSeason_2 = 'F13'
    END

It is possible to calculate the @PriorSeason_1 and @PriorSeason_2 values so that the parameters are set similar to this?

   SET @SP_Season = 'F13'
   SET @PriorSeason_1 = (@SP_Season - 1)
   SET @PriorSeason_2 = (@SP_Season - 2)

Thanks in advance for the help!

Was it helpful?

Solution 2

Thanks for the help but as usual, I figured it out shortly after asking the question.

Now as the user of the report selects the appropriate season to display, the previous years are calculated based on the user input.

Declare @SP_Season varchar(50) = @Season 
Declare @PriorSeason_1 varchar(50)
Declare @PriorSeason_2 varchar(50)
Declare @YR numeric 
Declare @Sport varchar (50)

SET @YR = (SELECT RIGHT (SEASON,2) FROM TK_SEASON WHERE SEASON = @SP_Season)
SET @Sport = (SELECT SUBSTRING(SEASON,1,LEN(SEASON)-2) FROM TK_SEASON WHERE SEASON = @SP_Season)
SET @PriorSeason_1 = (@Sport + CONVERT(VARCHAR(100),@YR - 1))
SET @PriorSeason_2 = (@Sport + CONVERT(VARCHAR(100),@YR - 2))

OTHER TIPS

DECLARE @var VARCHAR(3) = 'F14'
SELECT SUBSTRING(@var, 1, 1)
SELECT SUBSTRING(@var, 1, 1) + CAST(CAST(SUBSTRING(@var, 2, LEN(@var) - 1) AS INT) - 1 AS VARCHAR)
SELECT SUBSTRING(@var, 1, 1) + CAST(CAST(SUBSTRING(@var, 2, LEN(@var) - 1) AS INT) - 2 AS VARCHAR) 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top