SQL Server - using CASE in WHERE clause

后端 未结 1 830
囚心锁ツ
囚心锁ツ 2021-01-21 19:25

I want to use CASE in WHERE clause, but it returns error.

Can I use CASE in where clause? or how to fix it ? Thanks

SELECT * F         


        
1条回答
  •  走了就别回头了
    2021-01-21 19:37

    In your case, you only need OR

    WHERE
        (
        acting_to is null 
        OR 
            (
            datediff(day, acting_from, acting_to) >= 90
            AND
            acting_to >= '2010-10-01'
            )
        )
    

    A case is for values, not conditions. The condition is outside the CASE expression

    eg

    CASE
        WHEN SomeCol = 'a' THEN ColA
        WHEN SomeCol = 'c' THEN ColC
        ELSE ColB
    END > 42
    

    0 讨论(0)
提交回复
热议问题