title: How to create a ternary condition on a bit field in T-SQL
body: <p>I have a SQLExpress table that includes a bit field for storing TRUE/FALSE state.</p>
  
  <p>Something like:</p>
  
  <pre><code>+----+---------+
  | ID | IsAlive |
  +----+---------+
  | 1  |    1    |
  | 2  |    0    |
  | 3  |   NULL  |
  | 4  |    1    |
  +----+---------+
  </code></pre>
  
  <p>Using that table as our example, I want to create one Stored Procedure that will do any one of the following:</p>
  
  <ol>
  <li>Retrieve all records.</li>
  <li>Retrieve only the records with <code>IsAlive=1</code>.</li>
  <li>Retrieve only the records with <code>IsAlive=0 or NULL</code>.</li>
  </ol>
  
  <p>I am trying to think of how I can create my query without having to write IF/ELSE conditions - It seems to me there is a better/cleaner way than to do something like this:</p>
  
  <pre><code>-- The ternary logic...
  -- 0 or NULL retrieves records where IsAlive = 0 or NULL
  -- 1 retrieves records where IsAlive = 1
  -- Otherwise return all records 
  
  -- sproc .....
  
      @IsAlive tinyint = 2 -- Return all records by default
  
      AS
      BEGIN
        IF(@SentToNTService = 0 OR @SentToNTService = 1)
         BEGIN
          SELECT *
          FROM MyTable
          WHERE IsAlive = @IsAlive;
         END
      ELSE -- Lame redundancy 
         BEGIN
          SELECT *
          FROM MyTable
         END  
      END
  </code></pre>
  
  <p>Is there another way of creating the same results without having to create two different queries as I did above?</p>