I want to get one value from a function using a case statement. I tried the following but it does not work:
CREATE FUNCTION [FATMS].[fnReturnByPeriod]
(
Declare a second variable and then set that value because you aren't resetting @Period.
For example:
DECLARE @Output AS INT
SELECT @Output = CASE @Period
WHEN 1 then 1
WHEN @Period > 1 AND @Period <= 7 THEN 1 -- Should be 2
WHEN @Period > 7 AND @Period <= 30 THEN 1 -- Should be 3
WHEN @Period > 30 AND @Period<= 90 THEN 1 -- Should be 4
WHEN @Period > 90 AND @Period <= 180 THEN 1 -- Should be 5
WHEN @Period > 180 AND @Period <= 360 THEN 1 -- Should be 6
ELSE 0 END;
RETURN @Output;
I have left it like this as I'm assuming you are going to change your values for each of these CASE statements.