Based on the season being letters followed by 2 numbers, you can extract the 2 digits using SUBSTRING
and then cast to a number type:
declare @Season as varchar(50) = 'WB13'
select CONVERT(INT, SUBSTRING(@Season, LEN(@Season) - 1, 2))
Question
Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?
Below is code used to set the parameters used for my report. @Season is the only parameter used in the report and drives the others used to build the related queries. I am trying to evaluate @SP_Season using if statements for season 'F13', 'F14' and >= 'F15'. I have F13 and F14 working but I cannot get F15 to work. I think it should be something like this:
IF (CONVERT(NUMERIC,@SP_Season, >= 15)
But, this obviously isn't working. Any help would be greatly appreciated.
(
@Season varchar(50)
)
WITH Recompile
as
BEGIN
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
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))
Solution
Based on the season being letters followed by 2 numbers, you can extract the 2 digits using SUBSTRING
and then cast to a number type:
declare @Season as varchar(50) = 'WB13'
select CONVERT(INT, SUBSTRING(@Season, LEN(@Season) - 1, 2))