How to use a case statement in scalar valued function in SQL?

前端 未结 6 1476
半阙折子戏
半阙折子戏 2020-12-31 04:26

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]
(

            


        
6条回答
  •  忘掉有多难
    2020-12-31 05:01

    There are two types of CASE expression: simple and searched. You must choose one or the other - you can't use a mixture both types in one expression.

    Try this:

    SELECT CASE
        WHEN @Period = 1 THEN 1
        WHEN @Period > 1 AND @Period <= 7 THEN 2
        WHEN @Period > 7 AND @Period <= 30 then 3
        -- etc...
        ELSE 0
    END
    

    Also, you need to assign the result to something as others have already pointed out.

提交回复
热议问题