How do I create a conditional WHERE clause?

前端 未结 4 768
无人共我
无人共我 2020-12-08 21:19

I need to have a conditional where clause that operates as so:

Select *
From Table
If (@booleanResult)
Begin
  Where Column1 = \'value1\'
End
Else
Begin
  W         


        
4条回答
  •  旧时难觅i
    2020-12-08 21:54

    To provide a shorter answer:

    Select *
    From Table
    Where Column1 = 'value1' and
          coalesce(column2, '') = (case when @BooleanResults = 0 
                                        then 'value1' 
                                        else coalesce( column2, '')
                                   end)
    

提交回复
热议问题