title: Conversion failed when converting the varchar value 'N' to data type int
body: <p>This is in SQL Server 2008 R2.</p>
  
  <p>I'm pulling my hair with this one. Follow closely, please.</p>
  
  <p>When I run this, I get 27 rows returned:</p>
  
  <pre><code>select * 
  from dbo.[12STD_NO_VISIT] 
  where (
           (dbo.fPhoneExists(PhoneNumber1) = 1  
           AND (NextCall1 BETWEEN GETDATE() AND DATEADD(hh, 1, GETDATE())))
        )
  </code></pre>
  
  <p>And when I run this, I get 21 rows returned (notice the change to PhoneNumber2 and NextCall2):</p>
  
  <pre><code>select * 
  from dbo.[12STD_NO_VISIT] 
  where (
            (dbo.fPhoneExists(PhoneNumber2) = 1  
             AND (NextCall2 BETWEEN GETDATE() AND DATEADD(hh, 1, GETDATE())))
        )
  </code></pre>
  
  <p>But, when I run this, 'ORing' the 2 conditions, I get an error:</p>
  
  <blockquote>
    <p>Conversion failed when converting the varchar value 'N' to data type int</p>
  </blockquote>
  
  <pre><code>select * 
  from dbo.[12STD_NO_VISIT] 
  where (
            (dbo.fPhoneExists(PhoneNumber1) = 1  
             AND (NextCall1 BETWEEN GETDATE() AND DATEADD(hh, 1, GETDATE())))
         OR  
            (dbo.fPhoneExists(PhoneNumber2) = 1
             AND (NextCall2 BETWEEN GETDATE() AND  DATEADD(hh, 1, GETDATE())))
        )
  </code></pre>
  
  <p>But it doesn't just give me the error. It first retrieves 42 rows, displaying that for a split second (Results tab), and then it displays the error (Messages tab).</p>
  
  <p>I can't figure this one out. Any help is very appreciated.</p>
  
  <p>Thanks!</p>
  
  <pre><code>FUNCTION [dbo].[fPhoneExists](@PhoneNumber varchar) 
  RETURNS BIT 
  WITH EXECUTE AS CALLER 
  AS 
  BEGIN 
  DECLARE @GoodNumber bit 
  IF (@PhoneNumber is NULL or @PhoneNumber = 0 or @PhoneNumber = '') 
    SET @GoodNumber = 0; 
  ELSE 
    SET @GoodNumber = 1; 
    Return(@GoodNumber); 
  END
  </code></pre>