title: How case in SELECT clause can affect the resultset in SQL Server?
body: <p>I'm facing a strange behavior in SQL Server. I have a query how return 233 rows and it's fine.</p>
  
  <p>The problem is if I add a condition in a CASE, well, she returns 48 rows. How is it possible ? CASE shouldn't change my results : it's a CASE in a SELECT clause and there is no WHERE on it.</p>
  
  <p>See the query below. The part between <strong>* ... *</strong> is the condition who change the result of the entire query.</p>
  
  <pre><code>SELECT * FROM (
  SELECT
  CASE 
      WHEN dq.type_of_cover = 'ndd' AND ah.type_of_cover = 'ndaa' THEN ah.type_of_cover 
      ELSE dq.type_of_cover 
  END AS type_of_cover,
  CASE 
      WHEN (dq.type_of_cover = 'CCD' AND dq.result_code = 'CCP') THEN NULL --a.mntgex
      WHEN (ah.type_of_cover = 'ndaa' *** AND ah.date = GETDATE() ***) THEN ah.first_amount
      ELSE ISNULL(dq.first_amount, 0)
  END AS problem,
  dq.active
  FROM (
      SELECT du.coddeb, du.coduti
      FROM cacheDebiteurConsolideByUtilisateur AS du
      WHERE du.coduti = 4102
  ) AS debi
  INNER JOIN DecisionQueue AS dq ON dq.coddeb = debi.coddeb
  OUTER APPLY (
      SELECT TOP 1 type_of_cover,  MIN(date) AS date, first_amount, coddeb, codadh 
      FROM AtradiusCLHistory
      WHERE codadh = 1003
      AND dq.coddeb = coddeb
      GROUP BY type_of_cover, first_amount, coddeb, codadh
  ) AS ah
  WHERE dq.codadh = 1003
  AND dq.cancelled != 1
  ) AS test 
  WHERE type_of_cover = 'ndaa' AND active = 1    
  </code></pre>
  
  <p>So when I got <strong>AND ah.date = GETDATE()</strong> in the "problem" CASE, I have 48rows, when not, 233.</p>