SQL use CASE statement in WHERE IN clause

前端 未结 7 1427
南方客
南方客 2020-12-06 00:17

Is it posible to use case in where in clause? Something like this:

 DECLARE @Status VARCHAR(50);
 SET @Status=\'published\';

 SELECT * FRO         


        
相关标签:
7条回答
  • 2020-12-06 00:25

    No you can't use case and in like this. But you can do

    SELECT * FROM Product P    
    WHERE @Status='published' and P.Status IN (1,3)
    or @Status='standby' and P.Status IN  (2,5,9,6)
    or @Status='deleted' and P.Status IN (4,5,8,10)
    or P.Status IN (1,3)
    

    BTW you can reduce that to

    SELECT * FROM Product P    
    WHERE @Status='standby' and P.Status IN (2,5,9,6)
    or @Status='deleted' and P.Status IN (4,5,8,10)
    or P.Status IN (1,3)
    

    since or P.Status IN (1,3) gives you also all records of @Status='published' and P.Status IN (1,3)

    0 讨论(0)
  • 2020-12-06 00:27

    I realize this has been answered, but there is a slight issue with the accepted solution. It will return false positives. Easy to fix:

    SELECT * FROM Products P    
    WHERE (@Status='published' and P.Status IN (1,3))
       or (@Status='standby' and P.Status IN  (2,5,9,6))
       or (@Status='deleted' and P.Status IN (4,5,8,10))
       or (@Status not in ('published','standby','deleted') and P.Status IN (1,2))
    
    • SQL Fiddle Demo

    Parentheses aren't needed (although perhaps easier to read hence why I included them).

    0 讨论(0)
  • 2020-12-06 00:34

    I believe you can use a case statement in a where clause, here is how I do it:

    Select 
    ProductID
    OrderNo,
    OrderType,
    OrderLineNo
    From Order_Detail
    Where ProductID in (
    Select Case when (@Varibale1 != '') 
    then (Select ProductID from Product P Where .......)
    Else (Select ProductID from Product)
    End as ProductID
    )
    

    This method has worked for me time and again. try it!

    0 讨论(0)
  • 2020-12-06 00:35
     SELECT  * FROM Tran_LibraryBooksTrans LBT  
     LEFT JOIN Tran_LibraryIssuedBooks LIB ON 
     CASE WHEN LBT.IssuedTo='SN' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1
          WHEN LBT.IssuedTo='SM' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 
          WHEN LBT.IssuedTo='BO' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1
     ELSE 0 END
    
    0 讨论(0)
  • 2020-12-06 00:42

    According to my Scenario, I did "use CASE statement in WHERE IN clause" like following

    @AdjType varchar(20) = 'Value', 
    @Base varchar(20) = 'Common'
    where 
    (select CASE WHEN SA.IsPersentage = 0 THEN 'Value' 
           WHEN SA.IsPersentage = 1 THEN 'Presentage' END) Like @AdjType
    and (Select CASE WHEN SA.IsDependOnBasicSalary = 0 THEN 'Common' 
            WHEN SA.IsDependOnBasicSalary = 1 THEN 'Basic Salary' END) like @Base
    
    0 讨论(0)
  • 2020-12-06 00:44
     select  * from Tran_LibraryBooksTrans LBT  left join
     Tran_LibraryIssuedBooks LIB ON   case WHEN LBT.IssuedTo='SN' AND
     LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 when LBT.IssuedTo='SM'
     AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 WHEN
     LBT.IssuedTo='BO' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1
     ELSE 0 END`enter code here`select  * from Tran_LibraryBooksTrans LBT 
     left join Tran_LibraryIssuedBooks LIB ON   case WHEN LBT.IssuedTo='SN'
     AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1 when
     LBT.IssuedTo='SM' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN 1
     WHEN LBT.IssuedTo='BO' AND LBT.LIBRARYTRANSID=LIB.LIBRARYTRANSID THEN
     1 ELSE 0 END
    
    0 讨论(0)
提交回复
热议问题