2013-02-12 9 views
6

Ciao a tutti (! Il mio primo post sul Stack),come utilizzare SQL WHERE CASE con NOT IN o uguale allo stesso tempo?

Questo funziona:

where 
    Tran_date between @FromDate and @ToDate 

and Range = @Range 

and Store_ID = 
    case when @Range = 'RangeName' then 
     1234 
    else 
     Store_ID 
    end 

ma come posso realizzare questo ?:

where 
    Tran_date between @FromDate and @ToDate 

and Range = @Range 

and Store_ID 
    case when @Range = 'RangeName' then 
     not in (1234, 5678) 
    else 
     Store_ID 
    end 

risposta

1

Penso che tu voglia:

AND NOT (@Range = 'RangeName' AND Store_ID IN (1234,5678)) 
0

ne dite di questo:

where Tran_date between @FromDate and @ToDate 
    and Range = @Range 

    and (@Range = 'RangeName' and Store_ID not in (1234, 5678) 
     or @Range <> 'RangeName' and Store_ID = Store_ID) 
4
where 
    Tran_date between @FromDate and @ToDate 

and Range = @Range 

and Store_ID 
    case when @Range = 'RangeName' AND Store_Id in (1234, 5678) 
     9999 -- Assumes 9999 is a non possible value. 
      -- If it is possible then pick one that isn't. 
    else 
     Store_ID 
    end