Tuesday, June 10, 2014

MSSQL - Case when statement in where condition

Where to use :

When we want to specify case when statements in where condition.

Sample:

select * from tblSample 
where  StatusId = CASE WHEN @iQAModeId = 1 THEN 5
                       when @iQAModeId = 2 then 
                       (
                           select MAX(StatusId) from                                        trnSplitAllocation where StatusId in                              (1,2) and SplitId = sp.SplitId                                                                         group by SplitId 
                       )
      when @iQAModeId = 3 then 
                       (
                           select MAX(StatusId) from                                        trnSplitAllocation where StatusId in                              (3,4) and SplitId = sp.SplitId                                                                         group by SplitId 
                       )                      
                       ELSE 0

                     END 

No comments:

Post a Comment