I cant not get my head around it. We have following simple query.
DECLARE @bFlag bit
SET @bFlag = 0
SELECT something = CASE
WHEN @bFlag = 1 THEN
CASE
WHEN RS.intInterval = 1 THEN '"Days"'
WHEN RS.intInterval = 2 THEN '"Weeks"'
WHEN RS.intInterval = 3 THEN '"Months"'
WHEN RS.intInterval = 4 THEN '"Years"'
END
Else
RS.intInterval
End
from MyTable AS RS WITH (NOLOCK)
So I want to get intInterval
(which is int
) if flag
is not set to true
. Otherwise if flag
is set to true
, I want to get Days
, Weeks
, etc depending upon value of intInterval
. If I run this with @bFalg = 1
, I get this error:
Conversion failed when converting the varchar value '"Weeks"' to data type int
which does not make any sense as I am not converting anything.
I know I can fix it by putting cast (intInterval as varchar)
in else
part. However I want to know the reason why I am getting this error, why case
is trying to convert 'Weeks' to int
?