title: SQL using case in a where clause
body: <p>What I have now works:</p>
  
  <pre><code>insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
      select
          @group_id,
          pt.PaymentTypeID
      from 
          PaymentType pt
      where charindex(pt.Title, @payment_type) &gt; 0
  </code></pre>
  
  <p>until @payment_type is null, then nothing gets inserted.</p>
  
  <p>Can anyone help me find an elegant way to do this?
  (where clause is pseudocode )</p>
  
  <pre><code>insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
      select
          @group_id,
          pt.PaymentTypeID
      from 
          PaymentType pt
      where 
          if @payment_type is null
          PaymentTypeID = 2
          else
          charindex(pt.Title, @payment_type) &gt; 0
  </code></pre>
  
  <p>Edit: I really appreciate the answers, but unfortunately, none of these answers update the table. This pseudocode may explain better what needs to happen. If @payment_type is null, then insert the groupID into the GroupId column, and a value of 2 into the PaymentTypeId column, else run the statement as normal. </p>
  
  <pre><code>        where 
          if @payment_type is null
          INSERT INTO GroupAcceptedPaymentType (GroupID, PaymentTypeID)
          VALUES (GroupId, '2')
          else
          charindex(pt.Title, @payment_type) &gt; 0
  </code></pre>
  
  <p>Edit: We found the solution, but it won't let me answer my own question(for 8 hours. Maybe I should set my alarm?), but here it is. Hopefully it helps someone:</p>
  
  <pre><code>if @payment_type &lt;&gt; ''
  begin
  insert GroupAcceptedPaymentType (GroupID, PaymentTypeID)
  select
      @group_id,
      pt.PaymentTypeID
  from 
      PaymentType pt
  where charindex(pt.Title, @payment_type) &gt; 0
  end
  else
  begin 
  INSERT INTO GroupAcceptedPaymentType (GroupID, PaymentTypeID)
      VALUES (@group_id, '2')
  end
  </code></pre>